Php Api html reponse display data issue using php - php

I am using an api to get scorecard of matches using php, response is coming from api as text/html so I conveted this response to json using json_encode first and then converted to json_decode for display but I am not getting matchId wise results.
API RESPONSE HEADERS
Connection: keep-alive
Content-Type: text/html
Date: Tue, 09 Dec 2014 13:36:11 GMT
Server: Mashape/5.0.5
Transfer-Encoding: chunked
X-Powered-By: PHP/5.2.17
demo: click here
var_dump($response);
Code Here
$html = json_encode($response);
$json = json_decode($html);
foreach($json as $data => $value)
{
echo $value->matchId;
}
above var_dump giving response, foreach loop printing nothing.
demo link scroll page at bottom you should see '------------' this message. foreach loop print start here

You need to specify if you want to get an associative array and not an object from json_decode . Therefore, you should use
json_decode($response, true)
For more information see here

Related

Getting a value from json response without header from cURL

I want a the value of the "token" from the json array from the response i get after performing a cUrl post request. after print_r($response); it prints out all that. but i just want the json string and one particular value with php.
Here is my response. I don't want all this before the response
HTTP/1.1 200 OK<br/>
Content-Type: application/json<br/>
Content-Length: 312<br/>
Connection: keep-alive<br/>
Vary: Accept-Encoding<br/>
Status: 200 OK<br/>
Cache-Control: max-age=0, private, must-revalidate<br/>
Date: Wed, 22 Mar 2017 12:52:25 GMT<br/>
Strict-Transport-Security: max-age=31536000<br/>
X-Request-Id: 9418df03bea4e4884522b703d0eec504<br/>
X-UA-Compatible: IE=Edge,chrome=1<br/>
ETag: "dd4de4d3a6e4e499d6a034ce784d2d76"<br/>
X-Runtime: 0.633173<br/>
X-Content-Type-Options: nosniff<br/>
X-Rack-Cache: invalidate, pass<br/>
X-Powered-By: Phusion Passenger 5.0.28<br/>
Server: nginx/1.10.0 + Phusion Passenger 5.0.28
{"response_code":"00","response_text":"Mobile wallet payment request has been issued.","description":"You will receive a bill prompt shortly on your number 0546653444 with invoice no. 201562656, kindly complete it.","transaction_id":"DTV408402","token":"8268dfffa46a16b0665a76","mobile_invoice_no":"201562656"}
You need to tell cURL that you don't want the header:
curl_setopt($ch, CURLOPT_HEADER, 0);
you can use php's explode() function on this response. Or you can adjust your curl request to not return headers with your requests.
$split=explode("\n\n",$response);
$array=json_decode($split[1],true);
var_dump($array);
if this does not work, modify explode function to split on "\r\n\r\n" instead of "\n\n";
you can do it with JSON decode.
$json = $Your_respone_variable;
$obj = json_decode($json);
print $obj->{'token'}; // token can be replaced by any other key of your JSON
For more informations take a look at:
http://php.net/manual/de/function.json-decode.php

Guzzle6 streams doesn't give me the exact output

I have been trying to integrate guzzle into my app and I am having trouble with it. So, I get my response object from
$response = $client->post(.....);
The response of the endpoint is in JSON format. However, when I try to use it I get the following:
HTTP/1.1 200 OK Cache-Control: no-store Pragma: no-cache
{
// JSON HERE
}
Because of that I can't just use:
$array = json_decode($response->getBody()->getContents());
How, can I get rid of it showing my the response header in the output of body?

How to return just JSON response from Laravel 4 REST API?

I have a Laravel RESTful API setup that needs to return data in JSON format to an app on one of my subdomains ( and thus not within the Laravel app itself ).
In the REST I am returning like:
return \Response::json(array(
"status" => "success",
"type" => "client",
"message" => "Nothing to see here!"
));
In my Class on the subdomain app I am simply trying to return this response to a view print the output for testing.
I would hope that I just get back JSON, but when I do a simple:
echo $resp;
I get nothing
If I do a:
print_r( $resp );
or
json_decode( $resp );
I get:
HTTP/1.1 200 OK
Server: nginx/1.6.2
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.4.35
Access-Control-Allow-Origin: *
Cache-Control: no-cache
Date: Sat, 06 Dec 2014 04:18:15 GMT
Set-Cookie: laravel_session=eyJpdiI6Ik5DdDFGK2tJQ3ZCVkJPQnRtZHRaU2c9PSIsInZhbHVlIjoiT2tXSTJWYzRKUXZncG9NOFZBYkhOUTZRaUg1c3NmUXlHTjF5bzYyaTZRYUY4S3VBMEdwdjdwdXNUNHkwblppV1I5YUFsY2dodGNyRHh5SmZYeGFjcWc9PSIsIm1hYyI6Ijc0Mzg3MGNlNTBiYTEyNTE3MzNhZDVjNjUzNzMwNzk0ZTMwN2MwYmFiNDIyMGE0N2MyNTQxMDNlMDdiOGFmOTQifQ%3D%3D; expires=Sat, 06-Dec-2014 06:18:15 GMT; path=/; httponly
{"status":"success","type":"client","message":"Nothing to see here!"}
I don't want all of that headers stuff in my response like that. Or I am receiving and trying to parse it incorrectly. What am I doing wrong here?
Ok, this was a dumb mistake not with the REST API, but with the cURL call to the API.
If this ever happens to you just check and make sure you do not have:
curl_setopt($ch, CURLOPT_HEADER, 1);
That will output the headers with the response and mess up your day. I hope this helps others.
My return is like this:
return Response::json(array($data));
Just store array to a variable and call that variable on to the response.

Retrieving api's response in json format

I am using the campaign monitor's api and trying to retrieve the response in a json format, which it is supposed to do. I echoed the response, here's an example :
string(342) "HTTP/1.1 400 Bad Request
Server: csw
Cache-Control: private, s-maxage=0
X-CS-Node: 25
Content-Type: application/json; charset=utf-8
P3P: CP="OTI DSP COR CUR IVD CONi OTPi OUR IND UNI STA PRE"
Date: Sun, 27 Jan 2013 12:35:10 GMT
Connection: close
Content-Length: 66
{"Code":250,"Message":"List title must be unique within a client"}"
I would like to get the only last line. How can I do that ? (I tried to explode the response with a white space, but it didn't change anything).
Edit : the documentation is here, I call a method method(), that returns the response $response = method() and I run echo($response)
You could use the preg_match function to search for the JSON by searching for the Regular Expression '/{.*}/'.
preg_match('/\{.*\}/', $response, $matches);
Based on this example code you should be doing:
$result = method(); # I'm assuming you over-simplified the code in your question and "method" isn't the real method name
echo $result->response; # Not echo $result;

Can't get JSON response being posted to server

I am working with an API allows me to send a text message to a particular number it will then post that text info to my server as a JSON response. It has me configure the call back url and it is posting something to it. This is what I am being told the call looks like:
POST /business/getSMS HTTP/1.1
x-hookmobile-message-id: 297173743
User-Agent: Jakarta Commons-HttpClient/3.1
Host: testing.com
Content-Length: 152
Content-Type: text/plain; charset=UTF-8
{
"timestamp":"Fri Jan 18 21:56:32 GMT 2013",
"text":"Test",
"from":"+11111111111",
"messageId":"297173743",
"type":"incomingSms",
"recipient":"+11111111111"
}
What I am having trouble figuring out is how in PHP I can grab the JSON response. I tried $_POST just to see if the posted data is there but I don't get anything. This is the first time I have done this so I am at a bit of a loss here. How do I grab the posted json?
You'll have to read the raw post data.
$postdata = file_get_contents("php://input");
$obj = json_decode($postdata);
//$obj->timestamp == "Fri Jan 18 21:56:32 GMT 2013" etc

Categories