I'm setting up a little webservice usable via an app on Android.
The function in the server is this:
$lat=43.0552592;
$lng=12.4483577;
$radius=2000;
$url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=italian&location=".$lat.",".$lng."&radius=".$raidus."&types=restaurant&sensor=false&key=".$apiKey;
$urlW = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=italian&location=43.0552592,12.4483577&radius=2000&types=restaurant&sensor=false&key=XXXXxxxXXxxXXxxxxXXX";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Get the response and close the channel.
$response = curl_exec($ch);
return $response;
The trouble is that if I query Google with the $url (as in the above example) it returns the JSON with 'INVALID REQUEST', but if the query at the first curl_opt is done with $curlW will works like a charm.
While debugging that I've discovered, making $url return, that gets every & converted (before the curl_init!) in &...!
So I've tried almost every PHP string function to force every & decode or replacing every &entities to & only without any result.
Any suggestion?
you've done something wrong,
$url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=italian&location=".$lat.",".$lng."&radius=".$raidus."&types=restaurant&sensor=false&key=".$apiKey
should be
$url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=italian&location=".$lat.",".$lng."&radius=".$radius."&types=restaurant&sensor=false&key=".$apiKey;
but i don't guess that is what is doing the problem, try using file_get_contents() instead of curl, so $response will be like this:
$response = file_get_contents($url);
Related
Im hitting this url
https://graph.facebook.com/1843624489016097?fields=link&access_token=EAAD3ZBKkIhYMBAL3KRi9eZBXlYJADZARRFMSe0Nc35WTP92X2etkccVqnjNcjJgKbd8ABtX5pyDPN0nAA7jORyjpOGexZCYp1Sf2iw0DJjCf8UkPiLwhuApSGDGZBvy5w7vk3U0Ba97FZA2DO7J4m4UjvbIolDaRP9TRpemEmLyQZDZD
with the below code in php,
$url ='https://graph.facebook.com/' . $connection->provider_id . '?fields=link&access_token=' . $connection->token;
$ch = curl_init($url);
$json= curl_exec ($ch);
I have this html coming from Facebook, I want to use only "Link" in this,
{"link":"https:\/\/www.facebook.com\/app_scoped_user_id\/YXNpZADpBWEdrUlJiUzc0SWFWZATI4SEVJUmJHTTJQVHU2M3owcTJLOHh5MnJYOTI0LWdMT3VFUC1veXNWdXBhM3o3RzdkQmV4cjNfTC1nSkdheGFhV19pWWU5T1ZAWSzlkN0NBTUl4NVZAKTE9oRjlFbjdObU5i\/","id":"1686741234967769"}1
I tried converting that into JSON but its not working, Its coming in same format, since im doing this in a API, im checking this under Postman, I did like this..
$request = json_encode($json, JSON_HEX_QUOT | JSON_HEX_TAG);
The format is not getting convert to json, Im doing in PHP Laravel.
There's a 1 at the end of the output, possibly you're echoing something extra that you shouldn't .
I suspect you expect curl to return the actual result but you are not using the appropriate flag. The reason I suspect that is because you are assigning the return result to $json but without the flag CURLOPT_RETURNTRANSFERwill return true and not any json value.
Here's what you can try:
$url ='https://graph.facebook.com/' . $connection->provider_id . '?fields=link&access_token=' . $connection->token;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER);
$json= curl_exec ($ch);
$jsonArray = json_decode($json, true);
$link = $jsonArray["link"];
More information on the curl flags in the manual
You should use json_decode in place of json_encode. So you should try like:
$request = json_decode($json, true);
$link = $request["link"];
Also use curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); to save response in variable just after curl_init.
I want to get data from api..
but I have trouble with json ..I can not get json string .. it print as normal string or xml..
I try multi method from net but no solution..
this my code:
$url = "http://fv4online.com/egov_api/v1/students/7";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept:
application/json;charset=UTF-8'));
// set url
curl_setopt($ch, CURLOPT_URL, $url);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
print_r($output);
$t=json_decode($output,true);
print_r($t);
// close curl resource to free up system resources
curl_close($ch);
result of print ($output) is string:
7345345343testtesttesttest2000testliterary4564620171testDamascusar_lang300600350en_lang160400200fr_lang160400200national80200100history120300200geograph120300200philosophy160400300religion802001501700155060successful
and result of print(var_dump($output)) is xml:
'
7345345343testtesttesttest2000testliterary4564620171testDamascusar_lang<'... (length=1544)
note: Previously I was able to get this data without any problems, but suddenly the problem arose
That string contains all values the XML from the url returns, just without the tags around them. If you do 'View Source' you'll see more.
You get XML, not JSON. Could be they've changed that (which would be a bit odd, but I've seen worse). If you want is as JSON:
$xml = simplexml_load_string($xml_string);
$json = json_encode($xml);
I'm trying to write a request with curl. I want to get the distance between two points. Therefore I'm getting the data of those points from my db to build the string. The final string looks like this when I echo it.
http://maps.googleapis.com/maps/api/distancematrix/json?origins=40215+Düsseldorf+Königsallee 59&destinations=40215+Düsseldorf&sensor=false
I also tried it with https instead of http but the result was the same.
As you can see, it returns a perfectly fine JSON-Response, but when I do this afterwards, I always get an error.
public function request($signedUrl) {
$ch = curl_init($signedUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$this->response = json_decode($data);
}
The $signedUrl is the requestUrl.
The error from Google I get, when I var_dump($data) is
400. That’s an error.
Your client has issued a malformed or illegal request. That’s all we know. "
When I var_dump the response, it just gives me null.
What could be the problem here and how could I fix it? I also tried to read it in with file_get_contents but without success
Works [Tested] . You got a bad request since the URL was not encoded.
<?php
$urlx='http://maps.googleapis.com/maps/api/distancematrix/json?origins=40215+D%C3%BCsseldor%20%E2%80%8Bf+K%C3%B6nigsallee59&destinations=40215+D%C3%BCsseldorf&sensor=false';
$ch = curl_init($urlx);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
var_dump(($data));
Im trying to decode a json string returned from flickr within my PHP code. Im using CURL but it keeps returning a string even when I wrap json_decode() around the json sring variable. Any ideas?
$api_key = '####';
$photoset_id = '###';
$query = 'http://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key='.$api_key.'&photoset_id='.$photoset_id.'&extras=url_o,url_t&format=json&jsoncallback=1';
$ch = curl_init(); // open curl session
// set curl options
curl_setopt($ch, CURLOPT_URL, $query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch); // execute curl session
curl_close($ch); // close curl session
var_dump(json_decode($data));
Your request URL ends with:
&format=json&jsoncallback=1';
The correct name of the parameter is nojsoncallback, so the right URL you should be using ends like this:
&format=json&nojsoncallback=1';
Change that and it should work.
Regards.
That's because the returned data is not valid JSON. Its valid JavaScript, though.
The returned data is wrapped inside a default callback function called jsonFlickrApi.
You need to get rid of the JSON callback which wraps the JSON inside a callback function which is then supposed to be executed on the client side. You need to do some string manipulation on the returned JSON to remove the default callback jsonFlickrApi and then pass it to json_decode
$api_key = '####';
$photoset_id = '###';
$query = 'http://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key='.$api_key.'&photoset_id='.$photoset_id.'&extras=url_o,url_t&format=json';
$ch = curl_init(); // open curl session
// set curl options
curl_setopt($ch, CURLOPT_URL, $query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch); // execute curl session
curl_close($ch); // close curl session
$data = str_replace( 'jsonFlickrApi(', '', $data );
$data = substr( $data, 0, strlen( $data ) - 1 ); //strip out last paren
$object = json_decode( $data ); // stdClass object
var_dump( $object );
Even better instead of using a format=json in your url, use format=php_serial and get a serialize string then you wont have to worry about valid formating from flickr and you get an array in return
$api_key = '####';
$photoset_id = '###';
$query = 'http://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key='.$api_key.'&photoset_id='.$photoset_id.'&extras=url_o,url_t&format=php_serial';
$ch = curl_init(); // open curl session
// set curl options
curl_setopt($ch, CURLOPT_URL, $query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch); // execute curl session
curl_close($ch); // close curl session
$output = unserialize ($data);
stack overflow saves the day again. I scoured the flickr documentation and found NO MENTION of this "nojsoncallback" paramater.
who makes such a feature by default, then doesn't tell anyone how to disable it?
even worse, why would it be written that you have to ENable it in order to DISable the function?!
ridiculous... but thanks for the heads up, this fixed my problem!
The details of nojsoncallback is at the bottom this page https://www.flickr.com/services/api/response.json.html
Can someone please tell me where is the error in this code?, I use a mobile iphone application to call a php script tha will send information to apple. Apple then will return a JSON object containing several values in an associative array.
I want to reach the 'status' value but every time I run the code in the phone, the php script sends me the complete apple's returned string. In the XCode debugger the received string looks like:
[DEBUG]... responseString :
{"receipt":{"item_id":"328348691",
"original_transaction_id":"1000000000081203",
"bvrs":"1.0", "product_id":"julia_01",
"purchase_date":"2009-10-05 23:47:00
Etc/GMT", "quantity":"1",
"bid":"com.latin3g.chicasexy1",
"original_purchase_date":"2009-10-05
23:47:00 Etc/GMT",
"transaction_id":"1000000000081203"},
"status":0}
but the only piece I care in the string is the "status" value.
I've already looked inside documentation but can't find the solution. I'm new in php but this getting too long. Here the script:
<?php
//The script takes arguments from phone's GET call
$receipt = json_encode(array("receipt-data" => $_GET["receipt"]));
//Apple's url
$url = "https://sandbox.itunes.apple.com/verifyReceipt";
//USe cURL to create a post request
//initialize cURL
$ch = curl_init();
// set the target url
curl_setopt($ch, CURLOPT_URL,$url);
// howmany parameter to post
curl_setopt($ch, CURLOPT_POST, 1);
// the receipt as parameter
curl_setopt($ch, CURLOPT_POSTFIELDS,$receipt);
$result = curl_exec ($ch);
//Here the code "breaks" and return the complete string (i've tested that)
//and apparently doesn't get to the json_decode function (i think something's wrong there, so code breaks here)
curl_close ($ch);
$response = json_decode($result);
echo $response->{'status'};
?>
Even if I don't put any echo at the end, the script still returns a complete string (odd to me)
Thank's in advance and apollogies if I insist again from another question
Try setting the RETURNTRANSFER option to 1 so you can capture the output from the requested URL as a string. It seems that the default behaviour of cURL is to output the result directly to the browser:
...
$ch = curl_init();
// set the target url
curl_setopt($ch, CURLOPT_URL,$url);
// howmany parameter to post
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // <---- Add this
// the receipt as parameter
curl_setopt($ch, CURLOPT_POSTFIELDS,$receipt);
...