I'm trying to get video urls from dailymotion.
i got JSON results and its valid tested with online tools but when I use js_decode & print_r it shows warning like
<?php
$content = file_get_contents("http://www.dailymotion.com/embed/video/x49oyt5");
$content = explode(',"qualities":', $content);
$json = explode(',"reporting":', $content[1]);
$json = $json[0];
$mycontent = file_get_contents($json);
$response = json_decode($mycontent, true);
print_r($response);
?>
I want to get video quality and video url from JSON.
You're using file_get_contents on what is actually already a JSON.
updated code, tested ;)
<?php
$content = file_get_contents("http://www.dailymotion.com/embed/video/x49oyt5");
$content = explode(',"qualities":', $content);
$json = explode(',"reporting":', $content[1]);
$json = $json[0];
$videos = json_decode($json,true);
//Cycle through the 1080 videos and print the video urls
foreach($videos[1080] as $video){
printf("Video type:%s URL:%s\n", $video['type'], $video['url']);
}
//Cycle through the 720 videos and print the video urls
foreach($videos[720] as $video){
printf("Video type:%s URL:%s\n", $video['type'], $video['url']);
}
?>
With array_keys($array) you can get all the keys from array, it will return an array with the keys.
<?php
$content = file_get_contents("http://www.dailymotion.com/embed/video/x49oyt5");
$content = explode(',"qualities":', $content);
$json = explode(',"reporting":', $content[1]);
$json = $json[0];
$mycontent = file_get_contents($json);
$response = json_decode($mycontent, true);
$qualities = array_keys($response)
print_r($qualities);
?>
Related
I have a .txt file called 'test.txt' that is a JSON array like this:
[{"email":"chrono#gmail.com","createdate":"2016-03-23","source":"email"}]
I'm trying to use PHP to decode this JSON array so I can send my information over to my e-mail database for capture. I've created a PHP file with this code:
<?php
$url = 'http://www.test.com/sweeps/test.txt';
$content = file_get_contents($url);
$json = json_decode($content,true);
echo $json;
?>
For some reason, it's not echoing the decoded JSON when I visit my php page. Is there a reason for this and can anyone shed some light? Thanks!
You use echo to print scalar variables like
$x = 'Fred';
echo $x;
To print an array or object you use print_r() or var_dump()
$array = [1,2,3,4];
print_r($array);
As json_decode() takes a JSON string and converts it to a PHP array or object use print_r() for example.
Also if the json_decode() fails for any reason there is a function provided to print the error message.
<?php
$url = 'http://www.test.com/sweeps/test.txt';
$content = file_get_contents($url);
$json = json_decode($content,true);
if ( json_last_error() !== JSON_ERROR_NONE ) {
echo json_last_error_msg();
exit;
}
You'll need to split that json string into two separate json strings (judging by the pastebin you've provided). Look for "][", break there, and try with any of the parts you end up with:
$tmp = explode('][', $json_string);
if (!count($tmp)) {
$json = json_decode($json_string);
var_dump($json);
} else {
foreach ($tmp as $json_part) {
$json = json_decode('['.rtrim(ltrim($json_string, '['), ']').']');
var_dump($json);
}
}
I have a json file which is read by php and i want to change one object of the json file using php now my code looks like this but it doesn't work so how should I do? (the obj_name in the son object should be modified to $name)
<?php
$json = $_POST['myobj'];
$data = json_decode($json,true);
$name = xxxxxxxxx;
$data['obj_name'] = "$name";
#json = json_encode($data);
$filename = xxxxxxxxxxxxx
$file = fopen($filename,'w+');
fwrite($file, $json);
fclose($file);
?>
The # is commenting out your code and it should be a $ to make it a variable.
So, change this:
#json = json_encode($data);
to this:
$json = json_encode($data);
As the code i tried and by trial removal to get json content out of the return is below
method i used.
$date= YYYYMMDD;
//example '20140113'
$handle = fopen('http://finance.yahoo.com/connection/currency-converter-cache?date='.$date.'', 'r');
//sample code is http://finance.yahoo.com/connection/currency-converter-cache?date=20140208 paste the url in browser;
// use loop to get all until end of content
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
the code return a given bulk in yahoo and json format
so remove the unknown format which is
"/**/YAHOO.Finance.CurrencyConverter.addConversionRates (" and ends with ");"
by
$contents = str_replace('/**/YAHOO.Finance.CurrencyConverter.addConversionRates(','',$contents);
$contents = str_replace(');','',$contents);
$obj = json_decode($contents,true);
then loop the content by
foreach($obj['list']['resources'] as $key0 => $value0){
}
I prefer to use file_get_contents to get the html and preg_match_all to cleanup the json, i.e.:
<?php
$json = file_get_contents("http://finance.yahoo.com/connection/currency-converter-cache?date=20140113");
preg_match_all('/\((.*)\);/si', $json, $json, PREG_PATTERN_ORDER);
$json = $json[1][0];
$json = json_decode($json,true);
foreach ($json["list"]["resources"] as $resource){
echo $resource["resource"]["fields"]["date"];
echo $resource["resource"]["fields"]["price"];
echo $resource["resource"]["fields"]["symbol"];
echo $resource["resource"]["fields"]["price"];
}
NOTE:
I've tested the code and it works as intended.
I would like to be able to extract a title and description from Wikipedia using json. So... wikipedia isn't my problem, I'm new to json and would like to know how to use it. Now I know there are hundreds of tutorials, but I've been working for hours and it just doesn't display anything, heres my code:
<?php
$url="http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&exintro&titles=google&format=json&explaintext&redirects&inprop=url";
$json = file_get_contents($url);
$data = json_decode($json, TRUE);
$pageid = $data->query->pageids;
echo $data->query->pages->$pageid->title;
?>
Just so it easier to click:
http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&exintro&titles=google&format=json&explaintext&redirects&inprop=url&indexpageids
I know I've probably just done a tiny thing wrong, but its really bugging me, and the code... I'm used to using xml, and I have pretty much just made the switch, so can you explain it a bit for me and for future visitors, because I'm very confused... Anything you need that I haven't said, just comment it, im sure I can get it, and thanks, in advance!
$pageid was returning an array with one element. If you only want to get the fist one, you should do this:
$pageid = $data->query->pageids[0];
You were probably getting this warning:
Array to string conversion
Full code:
$url = 'http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&exintro&titles=google&format=json&explaintext&redirects&inprop=url&indexpageids';
$json = file_get_contents($url);
$data = json_decode($json);
$pageid = $data->query->pageids[0];
echo $data->query->pages->$pageid->title;
I'd do it like this. It supports there being multiple pages in the same call.
$url = "http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&exintro&titles=google&format=json&explaintext&redirects&inprop=url";
$json = file_get_contents($url);
$data = json_decode($json, TRUE);
$titles = array();
foreach ($data['query']['pages'] as $page) {
$titles[] = $page['title'];
}
var_dump($titles);
/* var_dump returns
array(1) {
[0]=>
string(6) "Google"
}
*/
Try this it will help you 💯%
This code is to extract title and description with the help of Wikipedia api from Wikipedia
<?php
$url = 'http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&exintro&titles=google&format=json&explaintext&redirects&inprop=url&indexpageids';
$json = file_get_contents($url);
$data = json_decode($json);
$pageid = $data->query->pageids[0];
$title = $data->query->pages->$pageid->title;
echo "<b>Title:</b> ".$title."<br>";
$string=$data->query->pages->$pageid->extract;
// to short the length of the string
$description = mb_strimwidth($string, 0, 322, '...');
// if you don't want to trim the text use this
/*
echo "<b>Description:</b> ".$string;
*/
echo "<b>Description:</b> ".$description;
?>
I have managed to get the longitude and latitude of postcodes from Google Maps but I am unable to then get the distance between the two. The following url gives me some JSON:
https://maps.googleapis.com/maps/api/distancematrix/json?origins=51.6896118,-0.7846495&destinations=51.7651382,-3.7914676&units=imperial&sensor=false
But I can't strip it out using PHP:
<?php
$du = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=51.6896118,-0.7846495&destinations=51.7651382,-3.7914676&units=imperial&sensor=false";
$djd = json_decode(utf8_encode($du),true);
print("-".$djd."-");
?>
Anybody know why this is?
EDIT
The following worked just fine:
<?php
$pc1 = 'SA92NH';
$pc2 = 'HP270SW';
$url1 = "https://maps.googleapis.com/maps/api/geocode/json?address=".$pc1."&sensor=false";
$url2 = "https://maps.googleapis.com/maps/api/geocode/json?address=".$pc2."&sensor=false";
$url1_data = file_get_contents($url1);
$url2_data = file_get_contents($url2);
$json1_data = json_decode(utf8_encode($url1_data),true);
$json2_data = json_decode(utf8_encode($url2_data),true);
$longlat1 = $json1_data['results'][0]['geometry']['location']['lat'].",".$json1_data['results'][0]['geometry']['location']['lng'];
$longlat2 = $json2_data['results'][0]['geometry']['location']['lat'].",".$json2_data['results'][0]['geometry']['location']['lng'];
print($longlat1."<br>\n");
print($longlat2."<br>\n");
?>
Is this what you are looking for?
You need to get the data, you cannot only type the url.
In my example i use get file_get_contents.
<?php
$du = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?origins=51.6896118,-0.7846495&destinations=51.7651382,-3.7914676&units=imperial&sensor=false");
$djd = json_decode(utf8_encode($du),true);
print_r($djd);
?>
http://codepad.viper-7.com/LqxpJW
$url = 'https://maps.googleapis.com/maps/api/distancematrix/json?origins=51.6896118,-0.7846495&destinations=51.7651382,-3.7914676&units=imperial&sensor=false';
$content = file_get_contents($url);
$json = json_decode($content, true);
And after that access $json like an array !
Something like that:
echo $json['rows']['elements']['distance']['text'];