The cURL request was retried 3 times and did not succeed - php

I'm currently developing a rest API call using (Guzzle client / Laravel) to a custom API. I have tried this several times by adding wrong body. I'm getting the following error
cURL error 0: The cURL request was retried 3 times and did not succeed. The most likely reason for the failure is that cURL was unable to rewind the body of the request and subsequent retries resulted in the same error. Turn on the debug option to see what went wrong. See https://bugs.php.net/bug.php?id=47204 for more information. (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
Following are the details of my development environment:
Language : php,
Framework : Laravel 5.4,
Client : Guzzle,
OS : Ubuntu 16.04
I would like to know the reason behind this
Please kindly assist me on this matter,
Thanks
following is my code
{
$headers = [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer '.$this->accessTocken,
'Accept' => 'application/json',
];
$Body = [
'method' => 'AndroidApp',
'msisdn' => '94777400725',
];
$client = new Client();
$response = $client->post( $this->url, ['headers'=> $headers,
'json'=> $Body]);
}

$response->getBody()->rewind();
worked for me

Related

How to set trace-time option to CURL using GUZZLE

I'm using PHP + Laravel to send some requests.
I want to capture every thing from sending request to the end. So I did this:
$verbose_log_stream = fopen('./curl.log', 'a+');
$client = new GuzzleHttp\Client([
'curl' => [
CURLOPT_VERBOSE => true,
CURLOPT_STDERR => $verbose_log_stream,
],
]);
I realized CURL also have some more options for better tracing called --trace-ascii and --trace-time
Anyone knows how can I set these two trace options to the Guzzle CURL options?

Unable to fetch response from file_get_contents using PHP

Gurus,
I am trying to fetch tracking details using the austpost.com.au website for which I have written the below code however, it doesn't seem to fetch anything.
$austpost_url = 'https://digitalapi.auspost.com.au/shipmentsgatewayapi/watchlist/shipments/99702032243801004670904';
$options = array(
'method' => 'get',
'contentType' => 'application/json',
'muteHttpExceptions' => true,
'headers' => array ('Accept' => 'application/json, text/plain, */*',
'Accept-Encoding' => 'gzip, deflate, br',
'AP_CHANNEL_NAME' => 'WEB_DETAIL',
'api-key' => 'd11f9456-11c3-456d-9f6d-f7449cb9af8e',
'Connection' => 'keep-alive',
'Origin' => 'https://auspost.com.au',
'Referer' => 'https://auspost.com.au/mypost/track/'),
);
$context = stream_context_create($options);
$html = file_get_contents($austpost_url, false, $context);
echo $html;
Direct Tracking Link: https://auspost.com.au/mypost/track/#/details/99702032243801004670904
I found this by going to the NETWORK tab in Chrome and figured out which request is loading the tracking details. Basically I am just after getting the status of the shipment. Snapshot below:
Any help in this would be much appreciated as I am using PHP to drive my code where I want to update the WordPress posts statuses based on the shipment status (that I can handle) however, stuck here on how I can get this shipment status before I move on.
It is because you need a valid recaptcha token and proof that you are not a robot. Won't be that simple, as you think. The easiest, but paid method, would be to use some captcha solving companies, like https://2captcha.com (not a sponsor of that answer) and their PHP libs.
If you want to use their api, you need to auth with username and password via basic auth. More details in here: https://developers.auspost.com.au/apis/shipping-and-tracking/reference/track-items
First check the fopen is on or off , add the phpinfo(); code in main file and check this .
Otherwise use the Curl function instead of file_get_contents();

PHP - Guzzle Strange trouble

I am using last version of PGP Guzzle HTTP... Very simple test done :
use GuzzleHttp\Client;
$client = new Client();
$res = $client->post('https://MYAPI/cards/XXX/credit',[
'headers' => ['Authorization' => 'Token MYTOKEN'],
'json' => [
"amount" => 10,
"reason" => "Because"
]
]);
var_dump($res);
returned a Exception:
GuzzleHttp\Exception\ClientException : Client error: `POST
https://MYAPI/cards/XXX/credit` resulted in a `405 Method Not Allowed`
response:
{"detail":"Méthode \"GET\" non autorisée."}
Any idea ? the REST API server I am calling is made with Django DRF. But this is nonsense to think it might be incompatible....
Thanxs for any idea.
You get very clear error that the server doesn't accept POST request. I don't know, is it right or not. If have control over the server also, you check the code there, maybe it's just a bug in it.
Basically you cannot talk about compatibility or incompatibility, because it's just simple HTTP and it works. You can have an issue either in communications between you and the server developers or a bug in the server.
Well, it was neither Guzzle nor DRF whiche failed.... only one parameter in DRF urls that does not accept trailing / in rest entry points....
simply adding / after the credit url and everything worked fine....
Strange behaviour somehow for DRF to display this kind of misleading error message .

"Empty Payload. JSON content expected" error calling Microsoft Graph (Guzzle & PHP)

I am trying to call the Microsoft Graph API to reset a passcode on a device registered with Intune. Unfortunately when I go to make the call, I receive an error stating that the JSON Payload is empty. The specific endpoint does not require a JSON payload, in fact it says to not include a body at all.
I attempted to add some JSON to see if that would satisfy the error, and I still receive the same error message.
Here is the call I am making:
$client = new Client();
try{
$client->post('https://graph.microsoft.com/beta/managedDevices/12345resetPasscode', [
'headers' => [
'Authorization' => 'Bearer 12345',
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'json' => json_encode(['hello' => 'world']),
]
]);
} catch (\GuzzleHttp\Exception\ClientException $e) {
dd($e->getResponse()->getBody()->getContents());
}
Here is the error I am receiving:
"Bad Request: Empty Payload. JSON content expected."
https://i.stack.imgur.com/gwwtJ.png
Here is the API Documentation I am working off: https://developer.microsoft.com/en-us/graph/docs/api-reference/beta/api/intune_devicefe_manageddevice_resetpasscode
Using PHP 7 & Guzzle 6
Any help is appreciated!
I'm an engineer on the Microsoft Intune team, working on the integration between Microsoft Graph and Intune.
It looks like there is an error in the documentation (I will make sure that is fixed). The correct URL You should be using is:
https://graph.microsoft.com/beta/managedDevices/12345/resetPasscode
Where 12345 is a the id of the device.
Hope that resolves your issue
Peter

Magento REST api

I am trying to use magento rest api for customers. But when i authenticate the application, it gives me following error.
Invalid auth/bad request (got a 500, expected HTTP/1.1 20X or a redirect)
Service temporary unavailable
I am trying to fetch product collection for customer role.
$oauthClient->fetch($resourceUrl, array(), 'GET', array('Content-Type' => 'application/xml'));
The line of code throws exception.
Any help will be appreciated.
Have you tried adding an "Accept" header to your request? I ran into the same problem with the Magento API, tested it and found that the PHP OAuth client doesn't send any accept header by default. So try the following instead:
$oauthClient->fetch($resourceUrl, array(), 'GET', array('Content-Type' => 'application/xml', 'Accept' => 'application/xml'));
or
$oauthClient->fetch($resourceUrl, array(), 'GET', array('Content-Type' => 'application/xml', 'Accept' => '*/*'));
You can view more info about the exception by using the following when you set up the client:
$oauthClient->enableDebug();
...and then looking at the debug with:
$oauthClient->debugInfo
or
$oauthClient->getLastResponse
The other methods are documented here:
http://www.php.net/manual/en/class.oauth.php
I've used $oauthClient->fetch($resourceUrl, array(), 'GET', array('Content-Type' => 'application/json', 'Accept' => '*/*')); which works fine.

Categories