How can I fetch information about the app/song/video etc. from iTunes Store? - iphone

I need to get an info about the app/song/video by item id from iTunes Store.
I've found this
But it doesn't work with apps.
Is there any public API?
UPD: I can get info using this link
, but this is not a structured data it's just a markup for iTunes to display stuff. I can't rely on that - it can be changed anytime and is hard to parse because it has no consistent structure...

Apple now seems to offer a friendlier search service returning JSON. NB: the documentation does stipulate that the API is for use in connection with promoting the search results (i.e. it's designed for affiliate links).
Example, fetching info about an app if you know its Apple ID:
http://itunes.apple.com/lookup?id=[appleID]
General keyword search
http://itunes.apple.com/search?term=[query]

As far as I know (and I've done a lot of looking), there isn't a public API.
You're right that the HTML isn't semantically structured, so parsing it won't be very robust. But I think it's your only option. Here are a few links which might help :-
A Python script which parses reviews.
An Ars Technica article: Linking to the stars: hacking iTunes to solicit reviews.
An Inside iPhone article: Scraping AppStore Reviews.

There is a public API into iTunes called "iTunes Store Web Service Search API" that returns quite a bit of information. Some of it is documented here but that documentation is incomplete.
You can use the API to get information about everything for sale in iTunes Store and App Store including urls for the artwork, links directly into iTunes, all the apps by a developer, and so on. It's very robust and I'd love to find updated documentation.
I'm currently writing an article at the iPhone Dev FAQ to show how a few things are done and extend the available documentation.

That link you have there is JSON! You've got the solution right here. You just need JSON.framework

I wrote this script for myself. It's not optimized or future-proof, but it's working for me in the meantime...
<?php
ini_set('display_errors', false);
if(isset($_GET['appID']) && isset($_GET['format']))
{
$appID = (int)stripslashes($_GET['appID']);
$format = stripslashes($_GET['format']);
$url = "http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=$appID&mt=8";
$useragent = "iTunes/4.2 (Macintosh; U; PPC Mac OS X 10.2";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
$result = curl_exec($ch);
curl_close($ch);
$temp = str_replace("½","",strip_tags(substr($result,
strpos($result,"Average rating for the current version:"),
strpos($result,"Rate this application:")-strpos($result,"Average rating for the current version:"))));
$temp1 = explode("ratings",$temp);
if(strpos($temp1[2], "Average rating for all versions:"))
$temp1[2] = substr($temp1[2],0,stripos($temp1[2],"Average rating for all versions:"));
$temp1[2] = preg_replace('/\s\s+/', ' ', $temp1[2]);
$temp2 = explode(" ",$temp1[2]);
$ratings[0] = $temp2[1];
$ratings[1] = $temp2[2];
$ratings[2] = $temp2[3];
$ratings[3] = $temp2[4];
$ratings[4] = $temp2[5];
if($format == "prettyPrint")
printRatings($ratings);
else if($format == "XML");
getXML($ratings);
}
else
{
echo "Enter the app id and format (http://iblackjackbuddy.com/getAppRatings.php?appID=###&format=###";
}
function printRatings($ratings)
{
echo "Five stars: " . $ratings[0];
echo "<br>Four stars: " . $ratings[1];
echo "<br>Three stars: " . $ratings[2];
echo "<br>Two stars: " . $ratings[3];
echo "<br>One star: " . $ratings[4];
echo "<hr>Total ratings: " . getTotalRatings($ratings);
echo "<br>Average rating: " . getAverageRating($ratings);
}
function getTotalRatings($ratings)
{
$temp = 1;
for($i=0; $i < count($ratings); ++$i)
$temp+=$ratings[$i];
return $temp;
}
function getAverageRating($ratings)
{
$totalRatings = getTotalRatings($ratings);
return round(5*($ratings[0]/$totalRatings)
+ 4*($ratings[1]/$totalRatings)
+ 3*($ratings[2]/$totalRatings)
+ 2*($ratings[3]/$totalRatings)
+ 1*($ratings[4]/$totalRatings),2);
}
function getXML($ratings)
{
header('Content-type: text/xml');
header('Pragma: public');
header('Cache-control: private');
header('Expires: -1');
echo '<?xml version="1.0" encoding="utf-8"?>';
echo '<Rating>';
echo '<FiveStars>'.$ratings[0].'</FiveStars>';
echo '<FourStars>'.$ratings[1].'</FourStars>';
echo '<ThreeStars>'.$ratings[2].'</ThreeStars>';
echo '<TwoStars>'.$ratings[3].'</TwoStars>';
echo '<OneStar>'.$ratings[4].'</OneStar>';
echo '<TotalRatings>'.getTotalRatings($ratings).'</TotalRatings>';
echo '<AverageRating>'.getAverageRating($ratings).'</AverageRating>';
echo '</Rating>';
}
?>

Related

Instagram Real-time API duplicate requests

I have an issue where when I create a real-time subscription I get duplicate notifications from different Instagram IP addresses. I have it set up so that when I get a notification, I send a request for latest updates using the min_tag_id setting. I store that in my db to use it for the next request. I don't always get duplicates, but when I do, everything about the notification is the same (time, object,changed_aspect), except I can tell they are different from my debugging output which lists two almost identical requests... the only differing info being a different IP address and the REQUEST_TIME_FLOAT is different by about 1/10th of a second. They even have the same HTTP_X_HUB_SIGNATURE value.
My general algorithm is:
process_subscription_update($data){
# get old min_id
$min_tag_id = mysqli_fetch_object(mysqli_query($dbconnecti,sprintf("SELECT instagram_min_id+0 as instaid FROM xxxx WHERE xxxx=%d",$_GET['xxxx'])));
$min_id = $min_tag_id->instaid;
# make api call
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, 'https://api.instagram.com/v1/tags/'.$_GET['tag'].'/media/recent?client_id=xxxx&min_tag_id='.$min_id.($min_id==0?'&count=1':''));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
curl_close($ch);
$i = json_decode($result);
if ($min_id == $i->pagination->min_tag_id) { exit; }
# write new min_id to db
record_min_id($i->pagination->min_tag_id);
$data2 = $i->data;
foreach($data2 as $d) {
process_instagram($d);
}
// debugging output: ****************
$file = file_get_contents($_SERVER['DOCUMENT_ROOT'].'instagram/updates.txt');
$foo = "\n";
foreach($_SERVER as $key_name => $key_value) {
$foo .= $key_name . " = " . $key_value . "\n";
}
$fulldata = $file . "\n\n\n" . $result . "\n min_id = " . $min_id . $foo;
$fulldata .= "\nTIME:".$data[0]->time;
$fulldata .= "\nOBJECT:".$data[0]->object;
$fulldata .= "\nCHANGED_ASPECT:".$data[0]->changed_aspect;
file_put_contents($_SERVER['DOCUMENT_ROOT'].'instagram/updates.txt', $fulldata);
// end debugging output *************
}
I'd like to avoid checking if the instagram message id already exists in my db within the process_instagram function, and with the duplicates only coming 1/10th of a second apart, I don't know if that would work anyway.
Anybody else experience this and/or have a solution?
I solved this. I don't think there is anything I can do about receiving the duplicate notifications. So, when writing the Instagram to my db, I have a field for the Instagram id and put a unique constraint on the field. After doing the mysqli INSERT, I check to see if the errno = 1062, and if it does, I exit.
mysqli_query($dbconnecti,"INSERT INTO xxx (foo, etc, instagram_id ...")
if ($dbconnecti->errno==1062) { exit; }
...
// more script runs here if we don't have a duplicate.

Adding an additional field to login with AWD Facebook Wordpress Plugin

I am using AWD Facebook wordpress plugin to allow my visitors to login with their Facebook account information. When a visitor registers on my site I automatically create a new post that is titled with their username and includes their Facebook profile picture as the content. The code for that is below:
function my_create_page($user_id){
$fbuide = 0;
$the_user = get_userdata($user_id);
$new_user_name = $the_user->user_login;
$new_user_avatar = get_avatar($the_user->user_email);
global $AWD_facebook;
$fbuide = $AWD_facebook->uid;
$headers = get_headers('http://graph.facebook.com/' . $fbuide . '/picture?type=large',1);
if(isset($headers['Location'])) {
$url = $headers['Location']; // string
} else {
$url = false;
}
$my_avatar = "<img src='" . $url . "' class='avatar AWD_fbavatar' alt='" . $alt . "' height='" . $size . "' />";
$my_post = array();
$my_post['post_title'] = $new_user_name;
$my_post['post_type'] = 'post';
$my_post['post_content'] = $my_avatar;
$my_post['post_status'] = 'publish';
wp_insert_post( $my_post );
}
add_action('user_register', 'my_create_page');
What I am looking to accomplish is a bit different though. I also want to include a brief biography about the user (currently the post is simply their picture). So when a visitor logs in with AWD Facebook, their needs to be an additional field that allows the user to type in their bio. Then I would be able to grab that info from their user profile and include it in the post. Any ideas on how I can accomplish this? Is there a different way to do this?
I would recommend storing their Facebook picture as metadata and use the content area as their bio for the automatically generated post. So something like this should get you started:
$my_post = array(
'post_title'=>$new_user_name,
'post_type'=>'post',
'post_content'=>'',
'post_status'=>'publish'
);
if( $id = wp_insert_post( $my_post ) ){
update_post_meta($id, 'avatar', $url);
}
Then you can generate the loop like so:
if ( have_posts() ) : while ( have_posts() ) : the_post();
//... stuff here
$avatar = get_post_meta($post->ID, 'avatar', 'true');
the_content();
echo '<img class="avatar AWD_fbavatar" src="'.$avatar.'" alt="'.$alt.'" height="'.$size.'" />';
endwhile;endif;

cURL throws an exception at Facebook API SDK

I'm trying to post an image as the user from my Facebook application.
Currently, this feature is still under development, and when I'm trying to test it using WAMP server on localhost, one of the cURL functions in the Facebook API SDK class, throws an exception error:
{"error_code":60,"error":{"message":"SSL certificate problem, verify
that the CA cert is OK. Details:\nerror:14090086:SSL
routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify
failed","type":"CurlException"}}null0.90752005577087
145684992172054|e259ec293812a5276a61d3181c395077
I have checked, and I have the correct settings at php.ini both at the Apache and PHP directories.
I tried to run this Both on Easy PHP with PHP 5.4 and on WAMP with PHP 5.3. (cURL extension enabled).
I have open SSL installed on both of the local servers.
I understand that you can solve this by changing the configuration of the CURL_opt, but modifying a Facebook base class, and compromising the security of my application (middle man attack), is really a last resort for me.
Thanks in advance for any help!.
My code:
test.php:
<?php
session_start();
require 'FB_auth.php';
require 'includes.php';
require 'functions_fb.php';
$start = (float) array_sum(explode(' ',microtime()));
//echo json_encode(photoToFacebook(299, 402, 134));
// echo json_encode(photoToFacebook(724, 402, 165));
// postToFacebook($db, 402, 134);
$end = (float) array_sum(explode(' ',microtime()));
$duration = ($end-$start);
echo $duration;
echo "<br/>" . $facebook->getAccessToken();
?>
functions_fb.php
<?php
function photoToFacebook($hr_id, $user_id, $job_id){
global $db, $FEED_PATH, $JOB_IMAGES_FOLDER , $facebook;
$path = $_SERVER['DOCUMENT_ROOT'] . "/";
$JOB_IMAGES_FOLDER = "images/job_images/";
$company = $db->getCompany($hr_id);
// perapre the job photo.
$photo = $db->getJobPhoto($job_id);
if (empty($photo)){
$photo = $db->getLogo($hr_id);
}
else{
$photo = $JOB_IMAGES_FOLDER . $photo;
}
try{
// At the time of writing it is necessary to enable upload support in the Facebook SDK, you do this with the line:
$facebook->setFileUploadSupport(true);
// Get album id if exists.
$album_uid = $db->getAlbumId($user_id);
$fb_user_id = $db->getFBUserId($user_id);
if (empty($album_uid)){
// Create an album
$album_details = array(
'message'=> 'Check out my company\'s job opportunities',
'name'=> "Job opportunities at {$company}"
);
$create_album = $facebook->api("/{$fb_user_id}/albums", 'post', $album_details);
echo json_encode($create_album);
$album_uid = $create_album["id"];
$db->saveAlbumId($user_id, $create_album["id"]);
}
$job_title = $db->getJobTitle($job_id);
$link = Bitly::make_bitly_url($FEED_PATH . "show_job.php?jid={$job_id}&th_uid={$user_id}&tp=1&pc=1", BITLY_APP_USERNAME,BITLY_APP_KEY,'json');
$photo_details = array(
'message'=> $job_title . "!\n" . $company . " is looking for you.\nOpen the link for details and apply.\n" . $link
);
$path .= $photo;
$photo_details['image'] = '#' . $path; //realpath($file);
// echo $album_uid;
$upload_photo = $facebook->api("{$fb_user_id}/photos", 'post', $photo_details);
// $db->updateAutoPostActivity($user_id, $job_id,1, json_encode($upload_photo));
// $db->updateAutoPostJob($job_id, $user_id);
return $upload_photo;
}
catch (FacebookApiException $e) {
echo "error:" . json_encode($e->getResult());
// $db->updateAutoPostActivity($user_id, $job_id,1, json_encode($e->getResult()));
// AtavMail::SendRichedMail(null , "", "Error on auto process - posting photo to FB", "Where - Job Id: {$job_id} , User Id: {$user_id}", json_encode($e->getResult()));
}
}
?>
I solved this by adding the following line into the base_facebook.php file:
public static $CURL_OPTS = array(
//
//
//
CURLOPT_SSL_VERIFYPEER => false,
);
Please notice that this is an development - environment solution only, and using this on your actual application can cause a security risk.
Please take the time to review the fullp proper solution on this link:
http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/
Setting SSL verification to false defeats the purpose of SSL.
A proper solution is to set the CA certs for CURL.
Solution 1 (Changes to PHP.ini):
Download CA bundle (cacert.pem) from http://curl.haxx.se/docs/caextract.html
Place it on your local system (for eg: C:\xampp\cacert.pem)
Open your php.ini
Set the curl.ca_info options to point to the location of the cacert.pem
Example: curl.ca_info="C:\xampp\cacert.pem"
Restart Apache
Solution 2 (Set options before each CURL call)
Download CA bundle (cacert.pem) from http://curl.haxx.se/docs/caextract.html
Place it on your local system (for eg: C:\xampp\cacert.pem)
Write the following code:
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt ($ch, CURLOPT_CAINFO, "pathto\cacert.pem");
Source: http://tumblr.wehavefaces.net/post/52114563111/environment-windows-xampp-curl-library

How to count likes on FB page?

I have to do a very simple operation but my programming skills are not enough. I have to count likes in Facebook page and print that number on my web-site. I have two scripts that do the job well for ordinary web-sites, but they don't want to show the number of likes for the page.
<?php
$source_url = "http://www.facebook.com/"; //This could be anything URL source including stripslashes($_POST['url'])
$url = "http://api.facebook.com/restserver.php?method=links.getStats&urls=".urlencode($source_url);
$likes = $xml->link_stat->like_count;
$comments = $xml->link_stat->comment_count;
$total = $xml->link_stat->total_count;
$max = max($shares,$likes,$comments);
echo $likes;
?>
<?php
$fql = "SELECT url, normalized_url, share_count, like_count, comment_count, ";
$fql .= "total_count, commentsbox_count, comments_fbid, click_count FROM ";
$fql .= "link_stat WHERE url = 'http://www.apple.com/'";
$apifql="https://api.facebook.com/method/fql.query?format=json&query=".urlencode($fql);
$json=file_get_contents($apifql);
print_r( json_decode($json));
?>
Both scripts work for ordinary web-sites but cant fetch fb page likes number. May be I should enter the link in another format or something?
I can get required data using graph like this http://graph.facebook.com/?ids=AutoSpecCenter , just by entering page name like that. But I don't know how to manipulate with this data.
As you already wrote in your question, you can query such information through Facebooks' Graph API. This short example will get the information of the Coca-Cola page, decode the JSON and outputs the number of people that like the page $data->likes.
<?php
$ch = curl_init("https://graph.facebook.com/CocaCola?access_token=<Access Token>");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$raw = curl_exec($ch);
curl_close($ch);
$data = json_decode($raw);
echo $data->likes . " people like Coca-Cola";
?>
If you need to perform more tasks than just getting the likes of a page, consider using the Facebook SDK as cpilko suggested.
Here's the quick and dirty way:
<?php
$fb_id = 'AutoSpecCenter';
$url = 'https://graph.facebook.com/' . urlencode($fb_id);
$result = json_decode( file_get_contents($url) );
printf("<p>There are %s people who like %s</p>", $result->likes, $result->name);
You'd do much better off installing the Facebook PHP SDK or using cURL to get this.
You could set $fb_id equal to a url as well.
here is a easy way too....
<?php
$page_id = 'yourfbpageid'; // your facebook page id
$xml = #simplexml_load_file("http://api.facebook.com/restserver.php?method=facebook.fql.query&query=SELECT%20fan_count%20FROM%20page%20WHERE%20page_id=".$page_id."") or die ("a lot");
$fans = $xml->page->fan_count;
?>
<li class="text white"><span></span><?php echo $fans.' Fans'; ?></li>
courtesy:ravi patel
This worked for me
<?php
function getData($username){
$access_token = 'YOUR ACCESS TOKEN'; //Replace it with your Access Token
$json = json_decode(file_get_contents("https://graph.facebook.com/".$username."?fields=fan_count&access_token=".$access_token),true);
return $json;
}
$data = getData("Mypage");//Replace it with your Username
$likes = $data['fan_count'];
echo "Facebook Likes: ".$likes;
?>
I was having same problem, just adding likes.summary(true),comments.summary(true) in parameter in against "fields" worked for me.
e.g. I used https://graph.facebook.com/me/feed?access_token=ACCESS_TOKEN&fields=story,from,story_tags,likes.summary(true),comments.summary(true)
instead of https://graph.facebook.com/me/feed?access_token=ACCESS_TOKEN
Also you can add other parameters if you want; separated by a ,
Also if you want count of single post you can use
https://graph.facebook.com/POST_ID/likes?summary=true&access_token=ACCESS_TOKEN
for likes count
Or
https://graph.facebook.com/POST_ID/comments?summary=true&access_token=ACCESS_TOKEN
for comment count
Try this code
<?php echo facebooklike('209414452444193');
function facebooklike($page_id){
$likes = 0; //Initialize the count
//Construct a Facebook URL
$json_url ='https://graph.facebook.com/'.$page_id.'';
$json = get_contents($json_url);
$json_output = json_decode($json);
//Extract the likes count from the JSON object
if($json_output->likes){
$likes = $json_output->likes;
}
return $likes;
}
function get_contents($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>

Twitter RSS feed, [domdocument.load]: failed to open stream:

i'm using the following:
<?php
$doc = new DOMDocument();
$doc->load('http://twitter.com/statuses/user_timeline/XXXXXX.rss');
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
);
array_push($arrFeeds, $itemRSS);
}
for($i=0;$i<=3;$i++) {
$tweet=substr($arrFeeds[$i]['title'],17);
$tweetDate=strtotime($arrFeeds[$i]['date']);
$newDate=date('G:ia l F Y ',$tweetDate);
if($i==0) { $b='style="border:none;"'; }
$tweetsBox.='<div class="tweetbox" ' . $b . '>
<div class="tweet"><p>' . $tweet . '</p>
<div class="tweetdate">#' . $newDate .'</div>
</div>
</div>';
}
return $tweetsBox;
?>
to return the 4 most recent tweets from a given timeline (XXXXX is the relevant feed)
It seems to work fine but i've recently been getting the following error sporadically:
PHP error debug
Error: DOMDocument::load(http://twitter.com/statuses/user_timeline/XXXXXX.rss) [domdocument.load]: failed to open stream: HTTP request failed! HTTP/1.1 502 Bad Gateway
I've read that the above code is dependant on Twitter beign available and I know it gets rather busy sometimes. Is there either a better way of receiving twits, or is there any kind of error trapping i could do to just to display "tweets are currently unavailable..." ind of message rather than causing an error. I'm usnig ModX CMS so any parse error kills the site rather than just ouputs a warning.
thanks.
I know this is old, but I was just searching for the same solution for a nearly identical script for grabbing a twitter timeline. I ended up doing this, though I haven't been able to thoroughly test it.
I defined the twitter url as a variable ($feedURL), which I also used in $doc_load. Then, I wrapped everything except for the $feedURL into this conditional statement:
$feedURL = "http://twitter.com/statuses/user_timeline/XXXXXXXX.rss"
$headers = #get_headers($feedURL);
if (preg_match("/200/", $headers[0])){
//the rest of you original code in here
}
else echo "Can't connect user-friendly message (or a fake tweet)";
So, it's just checking the headers of the the feed's page, and if its status is 200 (OK), then the rest of the script will execute. Otherwise, it'll echo a message of your choice.
(reference: http://www.phptalk.com/forum/topic/3940-how-to-check-if-an-external-url-is-valid-andor-get-file-size/ )
ETA: Or even better, save a cached version of the feed (which will also ensure you don't go over your API limit of loads):
<?php
$cache_file = dirname(__FILE__).'/cache/twitter_cache.rss';
// Start with the cache
if(file_exists($cache_file)){
$mtime = (strtotime("now") - filemtime($cache_file));
if($mtime > 600) {
$cache_rss = file_get_contents('http://twitter.com/statuses/user_timeline/75168146.rss');
$cache_static = fopen($cache_file, 'wb');
fwrite($cache_static, $cache_rss);
fclose($cache_static);
}
echo "<!-- twitter cache generated ".date('Y-m-d h:i:s', filemtime($cache_file))." -->";
}
else {
$cache_rss = file_get_contents('http://twitter.com/statuses/user_timeline/75168146.rss');
$cache_static = fopen($cache_file, 'wb');
fwrite($cache_static, $cache_rss);
fclose($cache_static);
}
//End of caching
?>
Then use $cache_file in your $doc->load($cache_file) statement instead of the actual feed url.
(Adapted from here: http://snipplr.com/view/8156/twitter-cache/).