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
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 made a RSS to JSON file
https://www.dannny0117.com/.well-known/api/news2.php
It works, in that it returns the output as JSON from the RSS source. Now, I want to print only a few elements from that with PHP echo.
According to the JSON, I need to grab channel, item, title and guid since those are the things I want to output.
I only need the 1st post title and link, but my code just won't pick it up because I don't fully know how to access the thing.
Here is the code that I'm using that I think should echo but doesn't:
<?php
$url = "https://www.dannny0117.com/.well-known/api/news2.php";
$content = file_get_contents($url);
$json = json_decode($content, true);
$title = $content['channel']['item'][0]['title'];
$link= $content['channel']['item'][0]['guid'];
echo $title , $link;
?>
This is not a problem with encoding or unicode character, the main problem is that my code CAN'T read the items from the JSON needed.
There are 2 mistakes which you made:
1) You have UTF-8 characters in your json string.
2) You have output of json_decode() string in variable $json but you are using $content.
Use the code below.
$url = "https://www.dannny0117.com/.well-known/api/news2.php";
$content = file_get_contents($url);
$enc = mb_detect_encoding($content);
if($enc == 'UTF-8') {
$content = preg_replace('/[^(\x20-\x7F)]*/','', $content);
}
$json = json_decode($content,true);
$title = $json['channel']['item'][0]['title'];
$link = $json['channel']['item'][0]['guid'];
echo "<pre>";
print_r([$title , $link]);
you can check this link here , it about predefined constants, usually when you deal with none latin letters you can have this kind of mess. try use JSON_UNESCAPED_UNICODE
and this will solve your problem.
you can use something like this:
$json_output = json_decode($content, true, JSON_UNESCAPED_UNICODE);
as you said it did not work so would you try to add this :
$options = array('http' => array(
'header' => 'Accept-Charset: UTF-8'
)
); $context = stream_context_create($options);
$url = "https://www.dannny0117.com/.well-known/api/news2.php";
$content= file_get_contents($url, false, $context);
$json = json_decode($content, true,JSON_UNESCAPED_UNICODE);
http://simsimi.com/getRealtimeReq?uuid=VbQqwsQ3qiCw1T04VrrFQzBBlkfZibg3YFkbWz6VquZ&lc=id&ft=1&reqText=Eh+jelek&status=W
How to get value from respSentence
Im using php lang ^_^
" {"status":200,"respSentence" 11 \:"respSentence"}"` is not valid json. Use some online sites to validate your json and try like this,
$json = '{"status":200,"respSentence" :"iyas"}';
$array = json_decode($json, true);
echo $array ['respSentence'];
You can try using file_get_contents and json_decode PHP functions
$url = "http://simsimi.com/........";
$data = file_get_contents($url);
$json = json_decode($data, true);
echo $json ['respSentence'];
hope it helps
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);
I am very new to PHP, and need to use an HTTP Request to download a URL that is external to my server. That URL is a PHP function that returns JSON code which I have decode. Any suggestions?
I have tried basic code:
<?php
//Code for forming the url (which I'm sure is correct)
$url = ...
$response = fopen($url,"x+");
$response = json_decode($response);
echo $response;
?>
//javascript in a seperate file that calls the php code
var response = xmlhttp.responseText;
alert(response);
Try this:
<?php
$url = 'YOUR_URL_HERE';
$data = file_get_contents( $url ); // it is a JSON response as per your statement.
$data= json_decode($data);
print_r($data); // now, it's a normal array.
?>
You may use fopen if config allows, or cURL or fsockopen functions to do that
You could use:
$json_str = file_get_contents($url);
$json = json_decode($json_str, true);
Couldn't you use file_get_contents? E.g.
<?php
$url = "YOUR_URL";
$json = file_get_contents($url);
// handle the data
$data = json_decode($json, TRUE);
var_dump($data); // example
?>
If you are doing a lot of requests you can try using a http class like Buzz which will Help clean up your code https://github.com/kriswallsmith/Buzz