Showing posts with label api tutorial. Show all posts
Showing posts with label api tutorial. Show all posts

Monday, November 28, 2011

Youtube Video Thumbnail Image

If you use YouTube to stream your videos, it will be a good idea to show users some thumbnail of the videos, so that there interest might increased. Fortunately YouTube provide five different thumbnails of each video. You really dot not have to store it on your server just call it directly from YouTube, all you need is your video id and the following YouTube Video Thumbnail URL.


http://img.youtube.com/vi/YouTube-Video-ID-HERE/0.jpg



http://img.youtube.com/vi/YouTube-Video-ID-HERE/1.jpg
http://img.youtube.com/vi/YouTube-Video-ID-HERE/2.jpg
http://img.youtube.com/vi/YouTube-Video-ID-HERE/3.jpg



http://img.youtube.com/vi/YouTube-Video-ID-HERE/hqdefault.jpg

simple isn't it!
Continue Reading...

Wednesday, July 20, 2011

Posting to Blogger Automatically

blogger api tutorial

In this post we will learn how to programmatically post to blogger, we use PHP for this but it can be done in any language which can send email. When we talk about programmatically, usually some complex programming scheme comes into mind but in this case there is no such thing, although one can use Blogger's PHP API or Blogger API for JAVA, but here we use simple mail to do this. First you need to configure your blogger account, open your blogger account and go to Setting -> Mobile and email as shown in figure below...

Posting to Blogger Automatically

in the Email section create an email which is your secret email address (do not share it with any one, mine show is figure is fake ;) ), this is used for creating blog post and set the option to your choice either you want it to publish immediately or save it as draft and then you approve it in future, in example i set publish immediately, after that click save settings and you blogger account is configured.

now create a simple PHP script that will send the mail and your post is automatically publish on blogger. A simple PHP code will be like this...
<?php

function sendPostToBlogger($postTitle,$postBody){

// our secret mail address we just created
$mailAddress="tutorialjinni.auto@blogger.com";

// using PHP mail function
mail($mailAddress,$postTitle,$postBody);

}
?>

and that is it... you can configure this is some sort of cron job or set it as a trigger in you code or as you wish :)

*Note You can pass HTML in $postBody too...
Continue Reading...

Friday, April 15, 2011

Detect Country from IP

ip to country
In this tutorial we will find country by IP address, usually a website required this functionality to give a personalized experience to its visitor that includes localization of content i.e. if a user comes from Pakistan the content should be in Urdu as this is national language of the country, or if it come form china content should be in Chinese so on and so forth, other reason may include, to give visitor ads according to there locality, a reason may to block visitors from certain country where a specific offer is not available and many more reasons...

Out main concern is how to do it? there are many ways of doing it
  • you can use $_SERVER["HTTP_ACCEPT_LANGUAGE"] it returns a comma separated values in the format "en-us,en;q=0.5" first value en-us tell its from US, it may en-uk: UK, en-jp: Janpan..
  • or you can use ip database lookup for it (we will discuss it)
  • you can buy a paid service (which i am not intrested)
  • or you can explicitly ask a user from where he belongs...
and may be more ways, do let me know if there are...

Now Let the Code Talk

to find Location from IP address i recommend using an IP database there are two ways of doing this, first download a IP database and install it on you own server and find location from there, and update it monthly or bi-monthly.
Second is more cool way of doing this using an API... go to ipinfodb.com sign up there and get API key and use the following code
function getLocationViaIP($IP_ADDRSS,$CITY=false){

    $KEY="YOUR_API_KEY_HERE";

    $TYPE=$CITY==true?"ip-city":"ip-country";
    // Check if city information is requried too

    $API_URL="http://api.ipinfodb.com/v3/$TYPE/?key=$KEY&ip=$IP_ADDRSS&format=xml";
    // Construst API URL
    // there two more formats json and raw if preder xml
    // there is another parameter callback if you use it
    // via javascript it will be handy
    
    $xml=simplexml_load_file($API_URL);
    
    if($xml->statusCode=="OK"){
        // Check if everything is OK
        
        echo "Country Name:".$xml->countryCode."< b r >";
        echo "Country Code:".$xml->countryName."< b r >";

        if($CITY){
            // Print City information too if requried
            echo "Region Name:".$xml->regionName."< b r >";
            echo "City Name:".$xml->cityName."< b r >";
            echo "Zip Code:".$xml->zipCode."< b r >";
            echo "Latitude:".$xml->latitude."< b r >";
            echo "Longitude:".$xml->longitude."< b r >";
            echo "Time Zone:".$xml->timeZone."< b r >";
        }
    }else{
        echo "Somthing Went Wrong";
    }
}
(break line tag intentionally written like this for display purpose)to call this method for example
$this->getLocationViaIP("74.125.45.100",true);

// alternativly you can use this too

$this->getLocationViaIP($_SERVER["REMOTE_ADDR"],true);

it will be a good idea that you store IP's Location somewhere in Cookies or in you local DB to minimize extra call to API.
Continue Reading...

Monday, April 4, 2011

Yammer API Example

yammer api php example
Yammer is an enterprise social network service that was launched in September 2008.Unlike Twitter, which is used for broadcasting messages to the public, Yammer is used for private communication within organizations or between organizational members and pre-designated groups, making it an example of enterprise social software. Yammer originally launched as an enterprise micro blogging service and has evolved to become a fully-fledged enterprise social network.

Yammer Offer its API to create personalized yammer applications for mobile phones and desktop, to in this tutorial we will make and application, authorize it and access data from user profile. To access data from yammer api one passed from following steps
  1. Register your Application on Yammer and get Application Secret and Application Key.
  2. Then get Token and Token secret from above data.
  3. Then Authorize your application using the token and get the Auth Code.
  4. Then Auth Code, token and token get the access token and access token secret.
  5. Finally get Data from the user profile.
And now get all the obvious stuff out of the way and let the code talk :) i made a small application demonstrating said steps.

yammer php api demo
Click on Image for full size

and the code...
<?php
error_reporting(0);

require_once 'common.inc.php';
require_once 'Yammer.php';
$appKey="YOUR APP KEY HERE";
$appSec="YOUR APP SECRET HERE";
$token="";
$tokenSec="";
$authcode="";
$accessToken="";
$accessTokenSec="";

$b=false;
if(isset ($_POST["isSubmit"])){

    $appKey=$_POST["appkey"]==""?"YOUR APP KEY HERE":$_POST["appkey"];
    $appSec=$_POST["appsec"]==""?"YOUR APP SECRET HERE":$_POST["appsec"];
    $token=$_POST["token"];
    $tokenSec=$_POST["tokensec"];
    $authcode=$_POST["authcode"];
    $accessToken=$_POST["accessToken"];
    $accessTokenSec=$_POST["accessTokenSec"];

    $test_consumer = new OAuthConsumer($appKey, $appSec, NULL);

    if(isset ($_POST["reqtoken"])){
        
        $endpoint="https://www.yammer.com/oauth/request_token";
        
        $req_req = OAuthRequest::from_consumer_and_token($test_consumer, NULL, "GET", $endpoint, null);
        $req_req->sign_request($plaintext_method , $test_consumer, NULL);

        $data=getContents($req_req->to_url());

        $data=  explode("&", $data);
        $token= str_replace("oauth_token=", "", $data[0]);
        $tokenSec= str_replace("oauth_token_secret=", "", $data[1]);
        $b=true;
    }
    if(isset ($_POST["acctoken"])){
        
        $endpoint="https://www.yammer.com/oauth/access_token";
        $param=Array('oauth_verifier'=>"$authcode");

        $test_token = new OAuthConsumer($token, $tokenSec);

        $acc_req = OAuthRequest::from_consumer_and_token($test_consumer, $test_token, "GET", $endpoint, $param);
        $acc_req->sign_request($plaintext_method, $test_consumer, $test_token);

        $data=getContents($acc_req->to_url());
        $data=  explode("&", $data);
        $accessToken= str_replace("oauth_token=", "", $data[0]);
        $accessTokenSec= str_replace("oauth_token_secret=", "", $data[1]);

    }
    if(isset ($_POST["getData"])){
        $obj=new Arc90_Service_Yammer($appKey, $appSec , $accessToken, $accessTokenSec, 10);
        $json=json_decode($obj->getMessagesAll());
        $k =count($json->messages);
        echo "

Messages Retrived via API

    "; for( $i=0;$i<$k;$i++){ echo "
  1. ".$json->messages[$i]->body->plain."
  2. "; } echo "
"; } } function getContents($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $data = curl_exec($ch); echo curl_error($ch); curl_close($ch); return $data; } ?>
and the html code ...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
input{
 font-size:24px;
 color:#666;
}
body {
font-size:20px;
font-family: Verdana,Arial,Helvetica,sans-serif;

}
.style7 {
 color: #990033;
 font-weight: bold;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Yammer OAuth & API Test -  Originative Systems</title>
</head>

<body>
    <form action="" method="post">
<table width="100%" border="1" style="border-collapse:collapse; border:solid 2px outset" cellpadding="8" cellspacing="5">
  <tr>
    <td colspan="5"><span class="style7">Yammer OAuth & API Test</span> </td>
  </tr>
  <tr>
    <td width="25%"><div align="right">Application Key : </div></td>
    <td width="75%" colspan="4"><label>
      <input name="appkey" type="text" value="<?php echo $appKey; ?>" id="appkey" size="40" />
    </label></td>
  </tr>
  <tr>
    <td><div align="right">Application Secrect : </div></td>
    <td colspan="4"><label>
      <input name="appsec" type="text" value="<?php echo $appSec; ?>" id="appsec" size="40" />
    </label></td>
  </tr>
  <tr>
    <td><div align="right">Token : </div></td>
    <td colspan="4"><label>
      <input name="token" type="text" value="<?php echo $token; ?>" id="token" size="40" />
    </label></td>
  </tr>
  <tr>
    <td><div align="right">Token Secrect : </div></td>
    <td colspan="4"><label>
      <input name="tokensec" type="text" value="<?php echo $tokenSec; ?>" id="tokensec" size="40" />
      </label>
      <?php if($b){
         echo "<INPUT type='button' value='Authorize Request' onClick=\"window.open('https://www.yammer.com/oauth/authorize?oauth_token=$token','Authorize Yammer Request','width=500,height=500')\"> ";
      }
      ?>
    </td>
  </tr>
  <tr>
    <td><div align="right">Auth Code : </div></td>
    <td colspan="3"><label>
      <input name="authcode" type="text" value="<?php echo $authcode; ?>" id="token" size="4"  />
    </label></td>
  </tr>
  <tr>
    <td><div align="right">Access Token : </div></td>
    <td colspan="4"><label>
      <input name="accessToken" type="text" value="<?php echo $accessToken; ?>" id="token" size="40" />
    </label></td>
  </tr>
  <tr>
    <td><div align="right">Access Token Secret : </div></td>
    <td colspan="4"><label>
      <input name="accessTokenSec" type="text" value="<?php echo $accessTokenSec; ?>" id="token" size="40" />
    </label></td>
  </tr>
  <tr>
    <td> </td>
    <td align="center"><label>
      <input name="reqtoken" type="submit" id="reqtoken" value="Request Token" />
    </label>
    </td>
    <td align="center"><label>
      <input name="acctoken" type="submit" id="acctoken" value="Access Token" />
    </label></td>
    <td align="center"><label>
      <input name="getData" type="submit" id="acctoken" value="Get Data" />
    </label></td>
  </tr>
</table>
        <input type="hidden" name="isSubmit" value=""/>
    </form>
</body>
</html>
just run it and you will understand it, there nothing left to explain after source code, download netbeans project with all the files used and enjoy :)

download yammer api
Download Yammer API Example

Continue Reading...

Monday, November 1, 2010

Share Audio on Facebook

Two days ago i was making a website which demands, that a song is to be publish on Facebook, the requirement is this that song must be played on the Facebook profile rather just to place a link on it. I (as usual) Google and try to find a solution or a tutorial that fit the bill but i did not find any, then I start reading the Facebook api page for sharing (that i bookmarked, though i do want to "read" that:) ), to my luck that was amazing and simple.

There is a very simple way to doing this you have just to put 4 meta tags in the head of the page and your done... simple isn't ?
<meta proprery="og:title" content="page title" /> 
<meta proprery="og:description" content="audio description" /> 
<meta property="og:image" content="audio image url (eg. album art)" /> 
<meta property="og:audio" content="audio url (.mp3 only)" /> 

AND

add a facebook share button of course.
Facebook Share
<script src='http://static.ak.fbcdn.net/connect.php/js/FB.Share' type='text/javascript'></script>
and you are good to go... if you want to see it in action visit musicjinni.com and listen any song or video and share it on facebok and see for yourself ...
Continue Reading...

Wednesday, August 18, 2010

Increase Twitter Followers Using PHP

Twitter is blooming these days, as you see more and more peoples and companies are joining and sharing there thoughts and updates instantly on twitter.There are many twitter advertising companies that tweet there advertisement through your tweet account (obviously you have to sign up with them and give them access to your account).Any one who use twitter knows well the importance of followers the greater the followers the greater is the chance that your voice is get heard and greater is the chance you get more relevant advertisement and make more money while you tweet.

People usually follow those who have large number of followers, usually it is difficult to have a large twitter followers base in the beginning.

there are services on the internet that offer different packages to increase number of followers so why spend your hard earned money on them, In order to get you quickly on track you can make some fake twitter accounts, once you make them there is no need to open you each account and individually follow your base account (the account you want to be followed).

i had written a small PHP program some time ago that will do the job, here it is.

<?php

set_time_limit(0);

$YOUR_USERNAME="ACCOUNT_WISHED_TO_BE_FOLLOWED";

$IDS="some1@company.com,some2@company.com,some3@company.com";

$PASSWORD ="SECRET_PASSWORD";

$arr=explode("",$IDS);

$total_no_of_twitter_ids=count($arr);

for($i=0;$i<$total_no_of_twitter_ids;$i++){

 echo ($i+1)." : ".trim($arr[$i])." -> "; 
 echo twitter_api_follow($YOUR_USERNAME,$arr[$i],$PASSWORD); 
 sleep(2);

}

function twitter_api_follow($YOUR_USERNAME,$ID,$PASSWORD){

 $ch = curl_init("http://twitter.com/friendships/create/" . $YOUR_USERNAME . ".json");
 
 curl_setopt($ch, CURLOPT_USERPWD, $ID.":".$PASSWORD);
 
 curl_setopt($ch, CURLOPT_POST, 1);
 
 curl_setopt($ch, CURLOPT_POSTFIELDS,"follow=true");
 
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
 $twitter_api_response = curl_exec($ch);
 
 return $twitter_api_response;

}

?>

if you are too lazy to write the code yourself you can download it form the link mentioned below.

Continue Reading...
 

Blog Info

A Pakistani Website by Originative Systems

Total Pageviews

Tutorial Jinni Copyright © 2015 WoodMag is Modified by Originative Systems