How to catch error in JSON response? - php

I'm getting the json data from the twitter using the wordpress function wp_remote_get as below:
$response = wp_remote_get('http://api.twitter.com/1/statuses/user_timeline.json?screen_name=twitter');
$json = json_decode($response['body']);
and it works fine. However, it totally creates the problem on my page if the twitter returns some error. In that case the content on my page does not load after that error part. So how can I catch the error and continue, only if there is no error.
For example if the twitter limit exceeds, it returns following response:
{"request":"\/1\/statuses\/user_timeline.json?screen_name=twitter","error":"Rate limit exceeded. Clients may not make more than 150 requests per hour."}
From the above responsive, how can i get the error.
I'm trying following, but it does not work (my page does not load at all)
if (!isset($json['error']){
//continue
}

It doesn't work because you are trying to access $json as an array, which it is not. It's an object, so use:
if (!isset($json->error){
//continue
}
Your page doesn't load because it's causing a fatal error, which in turn produces a 500 internal server error. If you check your error log you'll find:
Fatal error: Cannot use object of type stdClass as array in ....

You need to use json_decode to "transform" your "json string" in an object.
$j = json_decode($json);
echo $j->{'error'}
Here you will find everything what you need to know about json_decode: http://php.net/manual/en/function.json-decode.php

Related

Rendering JSON with Cakephp

I'm trying to return a Json object from an outside form with Cakephp 2.1. I have all my data, and I've looked at the responses, and the problem is I get a massive error text before my JSON!
Heres the code I use to print the json:
if (!$validationPassed) {
if ($ExternalRequest) {
if ($shouldEchoErrors){
Configure::write('debug',0); // Disabled so theoretically it can't even print errors
$this->RequestHandler->setContent('json', 'application/json' );
$json = json_encode($this->Customer->validationErrors);
echo $json;
$this->autoRender = false ;
}
The JSON it prints is correct, {"Email":["EmailUnique"]}. However I get a huge preformatted error message before that, stack trace and everything.
It was a dumb moment, but the answer in case anyone else finds this:
Theres an error far earlier in the code, with debug on, cake will print it immediately. This will get wrapped into the json output.
The specific error turns out to not be relevant for this question, its just in the view.

json_decode giving error in php for webservice call

I am consuming a webservice in PHP , below is code sample
> if ( $datas=file_get_contents($url) ) return json_decode($datas);
> return Array('error' => "Can't open url");
I return the the value of above function call to js page. but I dont get the value of webservice call in js but always get error value. However if I open the $url in browser I get below data (valid json) :
{"d":[ [ 5.305,428 ] ] }
Due to framework restriction i can use only json_decode() to return values back to my js page.
I also tried json_decode($datas,true); but still no help.
I also tried consuming same code in standalone php file :
I get this error : when I use json_decode($datas)
Catchable fatal error: Object of class stdClass could not be converted
to string in C:\xampp\htdocs\webservice-demo\clientRestTest.php on
line 32
and below error if I use json_decode($datas,true);
Notice: Array to string conversion in
C:\xampp\htdocs\webservice-demo\clientRestTest.php on line 32 Array
Please let me know how to resolve the issue .
Is there any special chars in $url? if there is, try urlencoding it first
file_get_contents(urlencode($url)); etc etc

Symfony 2.5 server returns 500 status

When I'm doing a request into API that I've written using Symfony 2.5. I'm sending form data using POST method and in the controller I'm doing
$params = json_decode($request->getContent(), true);
and if I do
var_dump($params);
I can see everything in console If I'm doing
console.log(data)
But If I try just do something like this:
array_keys($params)
Server returns me a 500 status code and error:
XMLHttpRequest cannot load http://host.loc/app_dev.php/posts. Invalid HTTP status code 500
You will always get 500 Error if something went completely wrong.
e.g. even use dump() in twig - which is not allowed in production
1) try the same page in /app_dev.php/[page]
- enable debug, this should give you alot of info about the error
2) look at the correct log file /app/logs/ as mentioned above
I guess you calling getContent() on a null Object ;-)

JSON not displaying with error in PHP

I'm looking to display Json from a url (http://pastebin.com/raw.php?i=MSyA8CBZ). Then, I want the "steamid" to be displayed. Currently, I am using this code, but it's not working:
<?php
$json = file_get_contents('http://pastebin.com/raw.php?i=MSyA8CBZ');
$obj = json_decode($json);
echo $obj->response->players->steamid;
?>
I get the error: Fatal error: Cannot use object of type stdClass as array in /home/u434538987/public_html/testjson.php on line 4 What is wrong with my code?
As mentioned in the comment, players is an array, is you could have seen if you had done print_r($obj) or even saw what the URL you load produced. So you should do :
$obj->response->players[0]->steamid;

always returns 200 as a status code from zend controller

I am creating custom class to throw a error from zend as there is not built-in mechanism for this in zend. I am able to give proper response to the user. But I am having problem in returning status code for this.
$obj = $this->toJsonModel($result);
$this->getResponse()->setStatusCode(403);
error_log('Status Code' . $this->getResponse()->getStatusCode());
return $obj;
I am getting response in $obj in Json format. Then I am setting status code to 403 for this API which I created. And then I am returning $obj from this controller.
But I am not getting 403 error when I tried this code. It's always showing 200 as a status code.
Some other code was causing a problem in executing this and was giving 200 every time but when I re-did some, it started working.

Categories