Request headers not sent with Zend Http

I make the following request using Zend \ Http \ Request and Zend \ Http \ Client

        //Creating request and client objects
        $request = new Request();
        $client = new Client();

        //Preparing Request Object
        $request->setMethod('POST');
        $request->setUri('https://my.api.address.com/apiendpointurl1234');
        $request->getHeaders()->addHeaders(array(
            'cache-control' => 'no-cache',
            'content-type' => 'application/json',
            'client_secret' => 'asdfasdfasdfasdfasdf',
            'client_id' => 'asdfasdfasdfasdfasdf',
            'accept' => 'application/json',
            'authorization' => 'Basic MTIzNDoxMjM0',
        ));
        $request->getPost()->set(json_encode(array(
            'student_id' => '123456',
            'short_description' => 'this is short description',
            'description' => 'this is detailed description of the question',
        )));

        //Sending Request
        $response = $client->send($request);

        var_dump( $response->getBody() );

However, the answer comes with this error:

"error": "headers: client_id required"

When I push the API through Postman, it works fine. But when I call this from a PHP application, it turns out that the headers are not sent correctly, which gives the above error. Does anyone know why headers are not being sent?

+4
source share
6 answers

try without addHeaders method

// Setting multiple headers, overwriting any previous value
$client->setHeaders(array(
    'Host' => 'www.example.com',
    'Accept-encoding' => 'gzip,deflate',
    'X-Powered-By' => 'Zend Framework'));

and maybe you have confusion with the client and request objects.

   $client = new Zend_Http_Client('http://example.org');
      $client->setHeaders(array(
        'Host' => 'www.example.com',
        'Accept-encoding' => 'gzip,deflate',
        'X-Powered-By' => 'Zend Framework'));
    $response = $client->request();

firebug, , ,

+1

Request, setRequest dispatch:

use Zend\Http\Client;
use Zend\Http\Request;

$request = new Request();
$request->setUri('https://my.api.address.com/apiendpointurl1234');

// Performing a POST request
$request->setMethod(Request::METHOD_POST);
$request->getHeaders()->addHeaders(array(
        'cache-control' => 'no-cache',
        'content-type' => 'application/json',
        'client_secret' => 'asdfasdfasdfasdfasdf',
        'client_id' => 'asdfasdfasdfasdfasdf',
        'accept' => 'application/json',
        'authorization' => 'Basic MTIzNDoxMjM0',
    ));

$client = new Client();

$client->setRequest($request);
$response = $client->dispatch();
+1

:

//Creating request and client objects
$request = new Request();
$client = new Client();

//Preparing Request Object
$request->setMethod('POST');
$request->setUri('https://my.api.address.com/apiendpointurl1234');
$request->getHeaders()->addHeaders(array(
    'cache-control' => 'no-cache',
    'content-type' => 'application/json',
    'client_secret' => 'asdfasdfasdfasdfasdf',
    'client_id' => 'asdfasdfasdfasdfasdf',
    'accept' => 'application/json',
    'authorization' => 'Basic MTIzNDoxMjM0'
));

$client->setOptions(['strictredirects' => true]);

$request->setContent(json_encode(array(
    'student_id' => '123456',
    'short_description' => 'this is short description',
    'description' => 'this is detailed description of the question',
)));

//Sending Request
$response = $client->send($request);

echo ($response->getBody());

, api URL, reset. , 943 Client.php,

$this->resetParameters(false, false);

. Postman, Postman .

, ,

$client->setOptions(['strictredirects' => true]);

.

PS: zend http, "$ request- > getPost() → set". , .

EDIT: , "client_id" api , , Zend\Http\Client, "Zend\Http\Client\Adapter\Socket", ( 361). , "Client_id" . 361 Zend\Http\Client\Adapter\Socket ":

 $v = ucfirst($k) . ": $v";

  $v = $k . ": $v";

, .

+1

, Headers getHeaders, , setHeaders Request.

$headers = $request->getHeaders();
$headers->addHeaders([ ... ... ]);
$request->setHeaders($headers);
+1

, , Zend Framework , getHeaders Headers, .

,

 $request->getHeaders()->addHeaders(array(
     'cache-control' => 'no-cache',
     'content-type' => 'application/json',
     'client_secret' => 'asdfasdfasdfasdfasdf',
     'client_id' => 'asdfasdfasdfasdfasdf',
     'accept' => 'application/json',
     'authorization' => 'Basic MTIzNDoxMjM0',
));

 $headers = $request->getHeaders();
 $headers->addHeaders(array(
     'cache-control' => 'no-cache',
     'content-type' => 'application/json',
     'client_secret' => 'asdfasdfasdfasdfasdf',
     'client_id' => 'asdfasdfasdfasdfasdf',
     'accept' => 'application/json',
     'authorization' => 'Basic MTIzNDoxMjM0',
 ));
 $request->setHeaders($headers);

:

URI Post, ,

$request->setUri('https://my.api.address.com/apiendpointurl1234');

// Performing a POST request
$request->setMethod(Request::METHOD_POST);

, , , , , Content-Length ,

$request->setContent(json_encode(array(
    'student_id' => '123456',
    'short_description' => 'this is short description',
    'description' => 'this is detailed description of the question',
)));

, script

//Creating request and client objects
$request = new Request();
$client = new Client();
$request->setUri('https://my.api.address.com/apiendpointurl1234');

// Performing a POST request
$request->setMethod(Request::METHOD_POST);
$client->setOptions(['strictredirects' => true]);

$request->setContent(json_encode(array(
    'student_id' => '123456',
    'short_description' => 'this is short description',
    'description' => 'this is detailed description of the question',
)));
$headers = $request->getHeaders();
$headers->addHeaders(array(
    'cache-control' => 'no-cache',
    'content-type' => 'application/json',
    'client_secret' => 'asdfasdfasdfasdfasdf',
    'client_id' => 'asdfasdfasdfasdfasdf',
    'accept' => 'application/json',
    'authorization' => 'Basic MTIzNDoxMjM0',
));
$request->setHeaders($headers);
+1

, Zend\Http\Request Zend\Http\Client (-) . cURL -. , , , cURL client-id client-id. , , . , , :

    // Get cURL resource
    $curl = curl_init();
    // Set some options - we are passing in a useragent too here
    curl_setopt_array($curl, [
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => 'https://my.api.address.com/apiendpointurl1234',
        CURLOPT_POST => 1,
        CURLOPT_HTTPHEADER => [
            'client_id: ' . 'asdfasdfasdfasdf',
            'client_secret: ' . 'asdfasdfasdfasdfasdf',
            'Authorization: ' . 'Basic MTIzNDoxMjM0',
            'Content-Type: ' . 'application/json',
        ],
        CURLOPT_POSTFIELDS => [
            'student_id' => '111',
            'short_description' => 'this is the short description',
            'description' => 'this is the long description this is the long description this is the long description'
        ],
    ]);
    // Send the request & save response to $resp
    $response = curl_exec($curl);
    var_dump("Service call id is: " . $response);
    // Close request to clear up some resources
    curl_close($curl);

, .

0

Source: https://habr.com/ru/post/1666975/


All Articles