json_decode giving error in php for webservice call - php

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

Related

"Use of undefined constant success - assumed 'success'" error parsing json response

im using google recaptcha and the way of my validation is through ajax, everything is working, I got a response
{
success: "false/true"
}
but i got an error saying "Use of undefined constant success - assumed 'success" on line 30, any ideas, help, suggestions, recommendations?
this is my code on the controller (the one i use in communicating the google recaptcha API)
$captcha=$request->input('g-recaptcha-response');
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=mysitekey&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success == false) //this is the line 30
{
return 'You are spammer ! Get the out';
}else{
return 'Thanks for posting comment.';
}
this is the line 30 "$response.success == false"
You missed a couple of steps.
First you need to json_decode the response you get from the API
$responseData = json_decode($response);
Next you need to properly address the "success" element from which I assume will be an array, but you will verify this with a var_dump().
var_dump($responseData);
If you get an array then the if clause will look like this:
if ($responseData["success"]==false)
On the other hand if you get an object in the responseData, the `if clause will look like this
if ($responseData->success==false)
The dot notation is for javascript, in php the dot is a concatentation operator.

php why get null json when request done by mobile app but retrieve correct data on web browser

this is a snapshot of the code which encode age array using json but it doesn't work well when request from mobile app it retrieve null .
i try to reach the solution and eventually i found that the error was because of the first line in php code page before php tag was empty ! so when i return a json it gives me null because of the empty line

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;

How to catch error in JSON response?

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

php SoapServer not print any xml document

all
I create a soap server use below code. I found it is a little hard to create wsdl document in PHP .so I decide use non-wsdl mode.
$soapServer = new SoapServer(NULL,array('uri'=>'http://com.test.env',
'encoding'=>'UTF-8'));
$soapServer->addFunction("workprocess_orders_api_add");
$soapServer->addFunction("workprocess_list_api_get_local_name");
$soapServer->handle();
but when I try to access this program, it print nothing. both web-browser and program(I use SoapClient)
$client = new SoapClient(NULL,array('uri'=>'http://com.test.env',
'location'=>'http://localhost/~breeze.kay/ams/workprocess/api/soap/',
'trace'=>1,
'style' => SOAP_DOCUMENT,
'use' => SOAP_LITERAL));
var_dump($client->__call('workprocess_orders_api_add',array())); //print null
var_dump($client->__getFunctions()); //print null
I tired call some function like this at client-side:
echo $client->sayHello('test');
I get error:
Fatal error: Uncaught SoapFault exception: [Client] looks like we got no XML document in /Users/breeze.kay/Sites/ad-test/soap.php:15
where is wrong ? I have no idea.:( the code looks ok. why there is no print?
OK, I tried it out.
the problem is the request url must append ?wsdl ,like this: http://localhost/wbs/api.php?wsdl
but I use url-rewrite for my sites. the rewrite rule break the original url struct.
by the way, dose somebody know where talk about the url rule for soap request?
I read some manual not find it.
The XML document not found error usually occurs when there is an empty space before php opening or after closing tag, try clearing that and test.

Categories