Understanding Guzzle: error that I do not understand

0

I'm trying to convert a code that works, to its version with Guzzle, but I do not get the desired result, and I think it's because of a lack of understanding of Guzzle v6.

If I execute the following code

$postfields = array(
    'identifier' => $this->username,
    'secret' => $this->password,
    'accesskey' => $this->accesskey,
    'action' => 'GetClients',
    'responsetype' => self::RESPONSE_TYPE,
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url.self::ENDPOINT);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields));

$response = curl_exec($ch);

if (curl_error($ch)) {
    die('Unable to connect: ' . curl_errno($ch) . ' - ' . curl_error($ch));
}

curl_close($ch);

// Decode response
$jsonData = json_decode($response, true);

But if I pass it to his model (the one I think) it fails.

$client = new GuzzleHttp\Client();

$response = $client->post($this->url.self::ENDPOINT, [
    'headers' => [
        'action' => 'GetClients',
        'identifier' => $this->username,
        'secret' => $this->password,
        'accesskey' => $this->accesskey,
        'responsetype' => self::RESPONSE_TYPE
    ]
]);

I reach an exception

GuzzleHttp\Exception\ClientException  : Client error: 'GET https://mydomain.com/intranet/includes/api.php' resulted in a '403 Forbidden' response:
result=error;message=Invalid IP 2.137.XXX.XX

On the other hand, I do not know how to understand the call (url that forms Guzzle) with the -get () method

    
asked by abkrim 14.11.2018 в 18:21
source

1 answer

1

The error is a cajón desastre. A misunderstanding of Guzzle, led me to use headers instead of form_params in the method post

$response = $client->post($this->url.self::ENDPOINT, [
    'form_params' => [
        'action' => 'GetClients',
        'identifier' => $this->username,
        'secret' => $this->password,
        'accesskey' => $this->accesskey,
        'responsetype' => self::RESPONSE_TYPE
    ]
]); 
    
answered by 14.11.2018 в 19:34