So maybe I am doing this wrong but I am trying to get information using the streeteasy api but when I use this code (below) it doesn't return anything. Any help would be greatly appreciated.
$url = file_get_contents('http://streeteasy.com/nyc/api/areas/search?q=soho&key=API_KEY&format=json');
$data = json_decode($url, true);
print_r($data);
Related
I'm trying to return a value from my API using PHP, api can be found here:
code is as follows:
Im not seeing the echo on my page, don't see any errors and I believing im reading the json in correctly. Any help appreciated!
<?php
$titleid = 2;
$url = "http://kmoffett07.lampt.eeecs.qub.ac.uk/serverSide/buildapi.php?id={$titleid}";
$response = file_get_contents($url);
$returnvalue = json_decode($response, true);
echo $returnvalue["Age"];
?>
From what I can tell, the json is not valid on the server side (due to the "connected to db" text in front of the {} part). I think it would be a good idea to fix the server side response json data, if possible!
For now, here is a way to get the value it looks like you are intending to retrieve:
<?php
$titleid = 2;
$url = "http://kmoffett07.lampt.eeecs.qub.ac.uk/serverSide/buildapi.php?id={$titleid}";
$response = file_get_contents($url);
$adjusted_response = str_replace('connected to db', '', $response);
$returnvalue = json_decode($adjusted_response, true);
echo $returnvalue['tv_shows']['Age'];
?>
Output:
$ php example.php
16+
If the server side json data is fixed, I think you could shorten the code to something like this:
<?php
$titleid = 2;
$url = "http://kmoffett07.lampt.eeecs.qub.ac.uk/serverSide/buildapi.php?id={$titleid}";
$response = file_get_contents($url);
$returnvalue = json_decode($response, true);
echo $returnvalue['tv_shows']['Age'];
?>
The thing is that $response is returned as string , in order to fix that you need to edit your backend and make it give the response without "connected to db"
I have tried to get data from this source, which is school detail on my country. but when i get the response, i cannot decode my response and it say Null. Idk why, but when i try to copy and paste my response result to hardcode, it can be decoded. why ?
i have tried all possible way to solve this, nothing work.
this is my code :
$client = new \GuzzleHttp\Client();
$res = $client->request('GET', 'http://jendela.data.kemdikbud.go.id/api/index.php/Csekolah/detailSekolahGET?mst_kode_wilayah=026700');
$response = $res->getBody()->getContents();
$result = json_decode($response); // this return NULL
//But when i going to return the $response, it show the response.
return $response;
I expect to access the data or maybe just decode my code, and it will help me a lot.
FYI, I'am using Guzzle 6 and Laravel 5.7 to work with this.
i hope someone can try too access it to and help me.
or maybe if you want to test it you can use Curl Ways :
$param = 'index.php/Csekolah/detailSekolahGET?mst_kode_wilayah=026700';
$url='http://jendela.data.kemdikbud.go.id/api/'.$param;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec ($ch);
return $response;
The json_decode document says:
NULL is returned if the json cannot be decoded or if the encoded data
is deeper than the recursion limit.
You can use json_last_error or json_last_error_msg function to determine the problem.
I can't get the whole response, it's timeout and terminated.
Remove the line return $response;
and try the following code:
$enc = mb_detect_encoding($response);
if($enc == 'UTF-8') {
$response = preg_replace('/[^(\x20-\x7F)]*/','', $response);
}
echo "<pre>";
print_r(json_decode($response,true));
Per the documentation:
NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit
Source: json_decode
Some debugging tips:
Make sure all the characters fall within utf8
Specify a depth value(higher than the default)
Make use of json_last_error see: json_last_error
If debugging tip 1 happens to be the cause of the issue, see if you can limit the return to not include the offending characters.
I am using CURL to send a URL. It's not sending an URL which contains PHP variables although is working perfectly on a defined URL (not incuding any PHP variable).
For example: This link is not working because of PHP variables :
$url = "http://abc/create/name/{$firstname} {$lastname}/email/{$email}/password/{$password1}?level={$level}&session=Dec";
$request = curl_init($url);
$response = curl_exec($request);
var_dump($response);
This works fine: Contains static values
$url = "http://abc/create/name/any one/email/anyone#gmail.com/password/12345?level=1&session=Dec";
$request = curl_init($url);
$response = curl_exec($request);
var_dump($response);
What am I doing wrong? Any leads?
Note: The URL is perfectly echoed, No error in echo $url
please putt like this and it will work
$url = 'http://abc/create/name/{'.$firstname.'}{'.$lastname.'}/email/{'.$email.'}/password/{'.$password1.'}?level={'.$level.'}&session=Dec"';
Finally got the solution :)
I am giving a space between in my {$firstname} {$lastname}thats why its not sending the URL.
So i just concatenate my firstname and lastname in a single variable and it works like a charm.
$fullname = $get_user_firstname.$get_user_lastname;
$fullname = urlencode($fullname);
$url = "http://abc/create/name/{$fullname}/email/{$email}/password/{$password1}?level={$level}&session=Dec";
Hope anyone with similar issue can get help from my this answer that's why i posted it. cheers!
I'm trying to extract data from an external url using json in PHP using the following code:
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=37.76893497,-122.42284884&sensor=false";
$json = file_get_contents($url);
$json_data = json_decode($json, true);
echo $json_data["formatted_address"];
?>
however, I get nothing on my page. in fact, i get this error:
Notice: Undefined index: formatted_address on line 7
is there something I'm missing?
any help would be greatly appreciated.
'formatted_address' is a key of the main array 'results', so you should loop $json_data['results'] and search for the key 'formatted_address'.
try this way,
echo $json_data['results'][0]['formatted_address'];
You are not providing proper INDEX. Proper INDEX is $json_data['results'][0]['formatted_address']; for 1st result.
Use foreach loop to print all address.
Try
$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=37.76893497,-122.42284884&sensor=false";
$json = file_get_contents($url);
$json_data = json_decode($json, true);
foreach($json_data['results'] as $item)
{
echo $item['formatted_address']."<br />";
}
I have a very simple bit of code
$pc1 = $_POST['post_code1'];
$pc2 = $_POST['post_code2'];
$url = "http://maps.google.com/maps/nav?q=from:".$pc1."%20to:".$pc2;
$url_data = file_get_contents($url);
$json_data = json_decode($url_data);
var_dump($json_data);
$url_data is full of juicy json stuff but $json_data returns NULL. Does anyone have an idea why?
I found the following worked after find a number of people with similar problems
$json_data = json_decode(utf8_encode($url_data),true);
source