i would like to see the post request packet before i send it as there is an error in the req and a the api is a general descript 500
error so i cant tell where y request is failing. i mnow the xml is formatted wrong as it works on postman from chrome.
$client = new GuzzleHttp\Client([
'base_uri' => 'https://elstestserver.endicia.com',
]);
$xml = 'changePassPhraseRequestXML=<ChangePassPhraseRequest> <RequesterID>lxxx</RequesterID><RequestID>1263055835</RequestID><CertifiedIntermediary><AccountID>lxxx</AccountID><PassPhrase>dfdsfsd</PassPhrase></CertifiedIntermediary><NewPassPhrase>fdfdsfdsfs</NewPassPhrase></ChangePassPhraseRequest>';
$data = array("ChangePassPhraseXML" => $xml);
$response = $client->post("/LabelService/EwsLabelService.asmx/ChangePassPhraseXML", [
'form_params' => $data
]);
this request works in postman for chrome the heres a working example of the xml
changePassPhraseRequestXML=<ChangePassPhraseRequest><RequesterID>lxxx</RequesterID><RequestID>1263055835</RequestID><CertifiedIntermediary><AccountID>lxxx</AccountID><PassPhrase>dfdsfsd</PassPhrase></CertifiedIntermediary><NewPassPhrase>fdsfdsfds</NewPassPhrase></ChangePassPhraseRequest>
Use Logger middleware
About middlewares for guzzle
Also, 500 error is server fail, not yours.
Related
I have a relatively simple guzzle request:
$client = new Client([
'base_uri' => $request_base
]);
$response = $client->request('POST', $request_path, [
'form_params' => ['key' => 'value']
]);
echo $response->getBody();
Right now I'm getting the body when it finishes downloading but I need to get the partial body every few seconds while it is being downloaded.
I looked at the documentation for a while, I'm guessing I have to make an async request but I'm not sure how I would access the partial body.
I can only use php and sockets, besides that I'm open to use another library/piece of code if it will mean a simple solution for this.
I'm trying to post a JSON variable and a string variable via HTTP Guzzle. It gives Internal Server Error stating unexpected ' (which is apparently ').
Here is what I have tried so far-
HTTP Guzzle Code
$data = $_GET['data'];
$email = $_GET['email'];
$client = new Client();
$response = $client->request('POST', 'http://someurlhere.com', [
'data' => $data, // this is json variable
'email' => $email // this is string variable
]);
if($response = $request->send()){
// redirect somewhere
}
I have also tried wrapping the JSON variable in 'json' => ['data' => $data], but nothing desirable happened and the error remained the same.
Also, the variables are not getting set via a form. So I have not wrapped them inside form_params.
I found what I was doing wrong there. The code is perfect. The only reason that was causing that problem was the errors I had on my other server, where I am doing the POST request.
This answer is just for my future reference and to help many others out there that might be facing the same problem or may face in the coming future.
I think you can use:
//Guzzle version ~6.3
$response = (new Client())->request("post", $uri, [
'json' => $formParams
]);
Check you $uri response directly with post man and resolve problem if it needed.
I have to do a DELETE request, with parameters, in the CodeIgnitor platform. First, I tried using cURL, but I switched to Guzzle.
An example of the request in the console is:
curl -X DELETE -d '{"username":"test"}' http://example.net/resource/id
But in the documentation of Guzzle they use parameters just like GET, like DELETE http://example.net/resource/id?username=test, and I don't want to do that.
I tried with:
$client = new GuzzleHttp\Client();
$client->request('DELETE', $url, $data);
but the request just calls DELETE http://example.com/resource/id without any parameters.
If I interpret your curl request properly, you are attempting to send json data as the body of your delete request.
// turn on debugging mode. This will force guzzle to dump the request and response.
$client = new GuzzleHttp\Client(['debug' => true,]);
// this option will also set the 'Content-Type' header.
$response = $client->delete($uri, [
'json' => $data,
]);
coming late on this question after having same.
Prefered solution, avoiding debug mode is to pass params in 'query' as :
$response = $client->request('DELETE', $uri, ['query' => $datas]);
$datas is an array
Guzzle V6
$response = json_decode($this->client->delete($uri,$params)->getStatusCode());
echo $response;
This will also give the status of the response as 204 or 404
I am getting a 401 on Guzzle 4.2 and the same setup works on Postman. Code below.
// Create a client with a base URL
$client = new GuzzleHttp\Client(['base_url' => 'cloud.feedly.com/v3/streams/contents?streamId=user/user-id/category/global.all&count=1']);
// Send a request to https://github.com/notifications
$response = $client->get();
//Auth
$response->addHeader('Authorization', "auth-code");
//send
$r = $response->send();
dd($r->json());
The error is:
GuzzleHttp \ Exception \ ClientException (401)
Client error response [url] cloud.feedly.com/v3/streams/contents?streamId=user/user-id/global.all&count=1 [status code] 401 [reason phrase] Unauthorized
Looking at the documentation page as per this line:
$response = $client->get();
It will send a get request, with no authorisation, hence the 401 response.
Try the following instead
// Create a client with a base URL.
$client = new GuzzleHttp\Client();
$request = $client-> createRequest('GET', 'cloud.feedly.com/v3/streams/contents?streamId=user/user-id/category/global.all&count=1');
$request->setHeader('Authorization', "auth-code");
// Send.
$response = $client->send($request);
dd($response->json());
The above creates a request, set the authorisation header on it. Then once prepared it actually sends it.
I think it worked in Postman because your headers are set, if you remove the authorization header it will likely fail there too.
I have not tested this but think it will work.
$response = $client->get('GET', 'cloud.feedly.com/v3/streams/contents?streamId=user/user-id/category/global.all&count=1', ['auth' => ['YOUR_USERNAME', 'YOUR_PASSWORD']]);
The 401 error means you should authenticate yourself with a valid username and password
Regards
I'm trying to consume the Stack Exchange API with Guzzle. I am facing an issue where I can't get the JSON response back: it apparently fails when parsing it.
Here is my code:
$client = new GuzzleHttp\Client();
$parameters = ['pagesize'=>'2','order'=>'desc','sort'=> 'activity','q'=>'laravel eloquent','site'=>'stackoverflow'];
$response = $client->get('http://api.stackexchange.com/2.2/search/advanced',['query' => $parameters ]);
The resultant effective URL that Guzzle creates is correct: if you open the link in your browser you'll see that it works fine and returns the requested data.
However, Guzzle fails with this error when trying to access the JSON with $response->json():
GuzzleHttp \ Exception \ ParseException
Unable to parse JSON data: JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded
After reading the documentation again, I believe that the request is compressed and I am not passing the appropriate content header. If this is so, can you please let me know which header I should be passing to get the correct response?
Ok so the following code works for me.
$client = new GuzzleHttp\Client();
$parameters = ['pagesize'=>'2','order'=>'desc','sort'=> 'activity','q'=>'laravel eloquent','site'=>'stackoverflow'];
$params = http_build_query($parameters);
$request = $client->createRequest('GET', 'http://api.stackexchange.com/2.2/search/advanced?'.$params);
$request->addHeader('Accept-Encoding','GZIP');
$request->addHeader('Content-Type','application/json');
$response = $client->send($request);
var_dump($response->json());