PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 24, 2022

[FIXED] How can I get response from guzzle in Laravel 5.3

 January 24, 2022     guzzle, laravel, laravel-5, php     No comments   

Issue

I try like this :

$client = new Client();
$res = $client->request('POST', 'https://api.orange.com/smsmessaging/v1/outbound/tel:+phone/requests/', [
            'headers' => [
                'Accept' => 'application/json',
                'Content-Type' => 'application/json',
                'Authorization'=>'Bearer '.$token,
                /*'Content-Type' => 'application/x-www-form-urlencoded',*/
            ],
            /*'form_params' => $body ,*/
            'json' => [
                'outboundSMSMessageRequest'=>[
                'address'=> 'tel:+$phone',
                'senderAddress'=>'tel:+phone_rec',
                'outboundSMSTextMessage'=>[
                     'message'=> 'Hello test!'
                ]
            ]],
            'debug'   => true,
            'verify' => false,
                ]
        );
        $res->getStatusCode();
        // 200
        $res->getHeader('content-type');
        // 'application/json; charset=utf8'
        $res->getBody();

When executed, the result is an errror curl_setopt_array(): cannot represent a stream of type Output as a STDIO FILE* How can I get the response?

I try in postman, it success get response

But I try use guzzle, it failed


Solution

You can try code below:

try {
    $client = new Client();
    $token = 'token';
    $res = $client->request('POST', 'https://api.orange.com/smsmessaging/v1/outbound/tel:+phone/requests/', [
            'headers' => [
                'Content-Type' => 'application/json',
                'Authorization'=>'Bearer '. $token,
            ],
            'json' => [
                'outboundSMSMessageRequest'=>[
                    'address'=> "tel:youre-phone",
                    'senderAddress'=>'tel:+phone_rec',
                    'outboundSMSTextMessage'=>[
                        'message'=> 'Hello test!'
                    ]
                ]],
            'debug'   => true,
            'verify' => false,
        ]
    );
    echo $res->getBody();
} catch ( \GuzzleHttp\Exception\ClientException $exception ) {
    echo $exception->getResponse()->getBody();
}


Answered By - Dmitry
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing