file_get_content function for getting the url content - php

i have decoded json to php array like this :
$text = "http://www.youtube.com/watch?v=i62Zjga8JOM";
$last_url = "http://youtubeinmp3.com/fetch/?format=JSON&video=".$text;
$mp3_url = file_get_contents($last_url);
$var = json_decode($mp3_url, true);
$echo $var['link'];
no it shows link in this format :
https://www.youtubeinmp3.com/download/get/?i=BUuT86WbAYpsj2zvLfkSb2%2BrQxjGl0bRMwnZiGFPR6pXuO1FXOec0doOUcihuH1yTinl2YgaypX3nnsahxx5KA%3D%3D
if you open this url a file will download. but on this code it just show s link not the content of that!
how can i do this ?

$text = "http://www.youtube.com/watch?v=i62Zjga8JOM";
$last_url = "http://youtubeinmp3.com/fetch/?format=JSON&video=".$text;
$mp3_url = file_get_contents($last_url);
$var = json_decode($mp3_url, true);
echo "<audio src=".$var['link']." controls></audio>";

Related

JSON to PHP Array using file_get_contents not working

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);
?>

Getting title, description and number of views of a youtube video

$youtube = simplexml_load_file('http://gdata.youtube.com/feeds/api/videos/wGG543FeHOE?v=2');
$title = $youtube->title;
This gets the title. But how could I get the viewcount and description? tried $youtube->description; and $youtube->views;
I suggest you to use the JSON output instead of the XML one.
You can get it by adding the alt=json parameter to your URL:
http://gdata.youtube.com/feeds/api/videos/wGG543FeHOE?v=2&alt=json
Then you have to load the json and parse it:
<?php
$json_output = file_get_contents("http://gdata.youtube.com/feeds/api/videos/wGG543FeHOE?v=2&alt=json");
$json = json_decode($json_output, true);
//This gives you the video description
$video_description = $json['entry']['media$group']['media$description']['$t'];
//This gives you the video views count
$view_count = $json['entry']['yt$statistics']['viewCount'];
//This gives you the video title
$video_title = $json['entry']['title']['$t'];
?>
Hope this helps.
UPDATE
To see what variables does the JSON output have, add the prettyprint=true parameter to the URL and open it in your browser, it will beautify the JSON output to make it more comprehensible:
http://gdata.youtube.com/feeds/api/videos/wGG543FeHOE?v=2&alt=json&prettyprint=true
Instead of browsing the URL you can just write
echo "<pre>";
print_r($json);
echo "</pre>";
after
$json = json_decode($json_output, true);
and it will print the formatted JSON output
My code:
$vId = "Jer8XjMrUB4";
$gkey = "AIzaSyCO5lIc_Jlrey0aroqf1cHXVF1MUXLNuR0";
$dur = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&id=".$vId."&key=".$gkey."");
$data = json_decode($dur, true);
foreach ($data['items'] as $rowdata) {
$vTime = $rowdata['contentDetails']['duration'];
$desc = $rowdata['snippet']['description'];
}
$interval = new DateInterval($vTime);
$vsec = $interval->h * 3600 + $interval->i * 60 + $interval->s;
if($vsec > 3600)
$vsec = gmdate("H:i:s", $vsec);
else
$vsec = gmdate("i:s", $vsec);
echo $vsec."--".$desc;
Result:
02:47--Following the critically acclaimed global smash hit X-Men:
Youtube API V2.0 has been deprecated.You must have to switch to V3 API.Which need API key.
So prefer to use but In this way so can not get video description
http://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=ktt3C7nQbbA&format=json

Can get JSON from Google Maps call

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'];

Remove "http://" from URL string

I am using a bit.ly shortener for my custom domain. It outputs http://shrt.dmn/abc123; however, I'd like it to just output shrt.dmn/abc123.
Here is my code.
//automatically create bit.ly url for wordpress widgets
function bitly()
{
//login information
$url = get_permalink(); //for wordpress permalink
$login = 'UserName'; //your bit.ly login
$apikey = 'API_KEY'; //add your bit.ly APIkey
$format = 'json'; //choose between json or xml
$version = '2.0.1';
//generate the URL
$bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$apikey.'&format='.$format;
//fetch url
$response = file_get_contents($bitly);
//for json formating
if(strtolower($format) == 'json')
{
$json = #json_decode($response,true);
echo $json['results'][$url]['shortUrl'];
}
else //for xml formatting
{
$xml = simplexml_load_string($response);
echo 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
}
}
As long as it is supposed to be url and if there is http:// - then this solution is the simplest possible:
$url = str_replace('http://', '', $url);
Change your following line:
echo $json['results'][$url]['shortUrl'];
for this one:
echo substr( $json['results'][$url]['shortUrl'], 7);
You want to do a preg_replace.
$variable = preg_replace( '/http:\/\//', '', $variable ); (this is untested, so you might also need to escape the : character ).
you can also achieve the same effect with $variable = str_replace('http://', '', $variable )

How to get user's profile pic url using graph api

Hi i m trying to get url of user's profile pic so I use following code
$userinfo = $facebook->api("/me/picture?type=large");
var_dump($userinfo);
but instead of returning url it returns NULL. Please help me why this is happening.
Thank you.
This is how we can get actual picture url
$URL='FB GRAPH API URL';
$headers = get_headers($URL, 1); // make link request and wait for redirection
if(isset($headers['Location'])) {
$URL = $headers['Location']; // this gets the new url
}
$url_arr = explode ('/',$URL);
$ct = count($url_arr);
$name = $url_arr[$ct-1];
$name_div = explode('.', $name);
$ct_dot = count($name_div);
$img_type = $name_div[$ct_dot -1];
$pos = strrpos($img_type, "&");
if($pos)
{
$pieces = explode("&", $img_type);
$img_type = $pieces[0];
}
$imagename = imgnameyouwant'.'.$img_type;
$content = file_get_contents($URL);
file_put_contents("fbscrapedimages/$imagename", $content);
If you know the [uid], you can simply use the following code
<img src="http://graph.facebook.com/[UID]/picture" />
You can also crop/resize the profile picture by providing height/width parameters.
Eg: https://graph.facebook.com/[UID]/picture?width=140&height=140
If you want to use standard sizes, try
<img src="http://graph.facebook.com/[UID]/picture?type=large" />
where type is one of {square,small,large}

Categories