So I'm integrating an API from a 3rd party company and I'm facing this strange situation.
I fetch the endpoint with the following code
$client = $this->client = new Client([
'base_uri' => 'https://xxx.xxxxxxxxxx.com/api/',
'timeout' => 15
]);
$this->requestConfig = [
'auth' => [
'xxxx#xx.xxx',
'xxxxx'
],
'headers' => [
'cache-control' => 'no-cache',
'content-type' => 'application/x-www-form-urlencoded'
],
];
$response = $this->client->get($url, $this->requestConfig);
$content = $response->getBody()->getContents();
Now the fun comes, if I var_dump content I get:
string(66) ""[{\"ExternalId\":\"38\",\"AgencyReference\":\"45436070356676\"}]""
Now I know this response is bad, response type if not set a json, json is URL encoded and everything smells bad.
I've been trying to parse this string for a while.
urldecode doesn't work either.
Question is simple, given a response like that, how can I get a normal array?
Currently using PHP 7.1
So finally found this:
Remove backslash \ from string using preg replace of php
To solve my issue.
In this case it was that the escaped quotes where malforming the json. My final code looks like this.
$response = $response->getBody()->getContents();
$clean = stripslashes($response);
$clean = substr($clean, 1, -1);
dd(json_decode($clean));
Please never write your API's like this...
Just looks awfull
BTW, Content-Type makes sense when you send (in request or response) some content. But you send a GET request, that has no content.
And to specify preferred content type for the response, you should use Accept HTTP header, Accept: application/json for example.
I'm not sure this will solve your problem, but just make things clear and correct ;)
Related
I am trying to post some data using a Guzzle Client Http request in Laravel. But for some reason the server respons with the message that it can't find the property Id in the JSON object, while the property Id is clearly in the request. The Guzzle documentation says that assigning the array to a json property in the request will result in a Json object being sent.
$url = "https://blablabla.com/api";
$key = "1234";
$data = [
'Id' => "4"
];
$response = Http::withHeaders([
"Authorization" => $key
])->post($url, [
'json' => $data
]);
Now I have tested the api in Postman and don't experience any problems. I even use the same api in a different php application using curl and it works perfect. So obviously there is something wrong with lines of code above and not with the api. I have tried different things but nothing works.. I have a feeling that the solution is so simple.. but for the last 6 hours I couldn't figure it out.. So please help before I go crazy :)
Laravel documentation on page HTTP Client says: "By default, data will be sent using the application/json content type". So you don't need to use 'json' property in post data. Just sent it directly: ->post($url, $data).
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 got a Problem. Im working on a Datatransfer to a Restful API at the Moment. All the arrays and everything and json objects should be fine, but i just do not understand how to determine the Content-Length of my Body?
$http = new Client([
'headers' => ['Authorization' => 'Bearer ' . $token["access_token"], 'Transfer-Encoding' => 'chunked', 'Content-Encoding' => 'chunked']
]);
This is my code at the moment. But the Answer thats coming back is always
HTTP Error 400. There is an invalid content length or chunk length in the request.
Can you help me to determine the right Content-Length?
Cheers!
There are at least 2 ways to do it.
If you have content in variable then add to your headers array 'Content-Length' => strlen($var)
You can also use output buffering and get length with function ob_get_length()
'Content-Length' => ob_get_length()
Here you will find example