I have a small problem with my tweets script. But for some reason, I don't know what the error could be. This is the error it gives me:
$url = "http://www.twitter.com/statuses/user_timeline/{$username}.xml?count={$number}";
$tweets = file_get_contents($url);
$feed = new SimpleXMLElement($tweets);
function time_stamp($date){
if (empty($date)){
return "No date provided";
}
and on the index.php page, it'll show this code:
<?php
$username = "user";//your twitter username
$number = 3;//number of tweets
include ("{$dir}/php/tweets.php");
?>
Do you guys know what it is that I'm doing wrong?
You don't need file_get_contents()
Try:
$url = "http://www.twitter.com/statuses/user_timeline/{$username}.xml?count={$number}";
$feed = simplexml_load_file($url);
Also, twitter made some changes not too long ago so your URL needs to look like this:
$url = "http://api.twitter.com/1/statuses/user_timeline/{$username}.xml?count={$number}";
Check this discussion.
You can use JSON easy and faster than XML
And to get content you can use
Curl => Faster
or
File_get_contents
Url
https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name={screenname}&count={count}
like this
<?php
$url = 'https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=abdullaheid&count=3'
$x = file_get_contents( $url ) ; // Using file get contents
$object = json_decode( $x ) ;
$array = (array) $object ;
print_r( $array ) ;
?>
Related
I'm new to php and i'm facing this problem. Need some help in matching the youtube views preg_match.
The below codes posted are the codes i made for the preg match but these are not working. Can anyone help me out.
if($row['type']=="Views"){
$data = $row['data'];
$type = "YouTube Views";
//INSTAGRAM
$file = #file_get_contents($data);
preg_match('/\"watch-view-count":(.*?)\,/',$file,$mfc);
$ccnt = $mfc[1];
//INSTAGRAM
}
if($row['type']=="Views"){
$data = $row['data'];
$type = "YouTube Views";
//Views
$file = #file_get_contents($data) or die("YouTube Offline ? :/");
preg_match('/\"watch-view-count":(.*?)\,/',$file,$mfc);
$ccnt = $mfc[1];
$data = http://www.youtube.com/watch?v=BPmhFrwDJTo
I need the preg_match to get the current count of views in the youtube video. I'm really sorry for the bad grammar.
Don't use regex for parsing HTML. Use a DOM Parser instead. It'll be more efficient.
In this case, you can simple use the Youtube API:
<?php
$video_ID = 'BPmhFrwDJTo';
$JSON = file_get_contents("https://gdata.youtube.com/feeds/api/videos/{$video_ID}?v=2&alt=json");
$JSON_Data = json_decode($JSON);
$views = $JSON_Data->{'entry'}->{'yt$statistics'}->{'viewCount'};
echo $views.' views';
?>
Output:
7 views
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'];
i want to get the subscriber count value from this JSON file: http://gdata.youtube.com/feeds/api/users/googlechrome?v=2&alt=json
This is what i did but it's not working.
$youtube_url = json_decode( file_get_contents( 'http://gdata.youtube.com/feeds/api/users/googlechrome?v=2&alt=json' ), true );
$youtube_data = $youtube_url['entry']['yt$statistics']['subscriberCount'];
PHP Code:
function get_yt_subs($username) {
$xmlData = file_get_contents('http://gdata.youtube.com/feeds/api/users/' . strtolower($username));
$xmlData = str_replace('yt:', 'yt', $xmlData);
$xml = new SimpleXMLElement($xmlData);
$subs = $xml->ytstatistics['subscriberCount'];
return($subs);
}
Example of usage:
PHP Code:
get_yt_subs('3moeslam')
I just change the JSON method to XML and everything work fine for me. the question that i wrote work good for #Matt Koskela but not for me. anyway, i'll go a head with this method but i really want to know the problem with the JSON method.
$youtube_url = file_get_contents( 'http://gdata.youtube.com/feeds/api/users/googlechrome';
$youtube_url = str_replace( 'yt:', 'yt', $youtube_url );
$youtube_data = $youtube_url->ytstatistics['subscriberCount'];
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 )