Wednesday, May 16, 2012

Working Yahoo PHP API


Get OAuth.php from  : http://oauth.googlecode.com/svn/code/php/OAuth.php


Install curl for PHP, remove the semi colon from "extension=php_curl.dll"



the query is given as a GET request or added in the URL as 


"http://xxx.php?q=searchterm"



Source : 


<?php


require("OAuth.php");  
   
$cc_key  = "YAHOO KEY HERE";  
$cc_secret = "SECRET KEY HERE";  
$url = "http://yboss.yahooapis.com/ysearch/web";  
$args = array();  
$q = $_GET["q"];
$args["q"] = $_GET["q"];  
$args["format"] = "xml";  
   
$consumer = new OAuthConsumer($cc_key, $cc_secret);  
$request = OAuthRequest::from_consumer_and_token($consumer, NULL,"GET", $url, $args);  
$request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, NULL);  
$url = sprintf("%s?%s", $url, OAuthUtil::build_http_query($args));  
$ch = curl_init();  


$headers = array($request->to_header());  
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);  
curl_setopt($ch, CURLOPT_URL, $url);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);  
$rsp = curl_exec($ch);  
//$results = json_decode($rsp);  


//To print out the entire results
//print_r($results);


$root = new SimpleXMLElement($rsp);
$results = $root->web->results->result;
echo "<h1>Yahoo</h1><br /><br />";
foreach($results as $result)
{


        echo <<<EOF
<h2><a href='{$result->clickurl}'>{$result->title}</a></h2><br />
<div>{$result->abstract}</div>


<hr />


EOF;
}
?>

Working PHP Youtube API


Thank you Google for a direct API code !

<?php

$q = "KEYWORD HERE";

require_once 'Zend/Loader.php'; // the Zend dir must be in your include_path

Zend_Loader::loadClass('Zend_Gdata_YouTube');
$yt = new Zend_Gdata_YouTube();
$yt->setMajorProtocolVersion(2);

function printVideoFeed($videoFeed)
{
  $count = 1;
  foreach ($videoFeed as $videoEntry) {
    //echo "Entry # " . $count . "<br /><br /><br />";
    echo "<br /><br /><br />";
    printVideoEntry($videoEntry);
    echo "\n";
    $count++;
  }
}

function printVideoEntry($videoEntry)
{
  // the videoEntry object contains many helper functions
  // that access the underlying mediaGroup object
  echo  $videoEntry->getVideoTitle() . "<br />";
  echo 'Description: ' . $videoEntry->getVideoDescription() . "<br />";
  echo 'Category: ' . $videoEntry->getVideoCategory() . "<br />";
  echo 'Tags: ' . implode(", ", $videoEntry->getVideoTags()) . "<br />";
  echo '<a href="' . $videoEntry->getVideoWatchPageUrl() . '">'.$videoEntry->getVideoTitle().'</a><br />';
  echo '<iframe width="420" height="315" src="' . $videoEntry->getFlashPlayerUrl() .'" frameborder="0" allowfullscreen></iframe>';

}

$query = $yt->newVideoQuery();
  $query->setSafeSearch('moderate');
  $query->setMaxResults(5);
  $query->setVideoQuery($q);

  // Note that we need to pass the version number to the query URL function
  // to ensure backward compatibility with version 1 of the API.
  $videoFeed = $yt->getVideoFeed($query->getQueryUrl(2));
  printVideoFeed($videoFeed, 'Search results for: ' . $q);
?>

Working PHP Amazon API

Save this in a file named "aws_signed_request.php" :

Source : http://mierendo.com/software/aws_signed_query/


<?php

function aws_signed_request($region, $params, $public_key, $private_key)
{
    /*
    Copyright (c) 2009 Ulrich Mierendorff

    Permission is hereby granted, free of charge, to any person obtaining a
    copy of this software and associated documentation files (the "Software"),
    to deal in the Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish, distribute, sublicense,
    and/or sell copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
    THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    DEALINGS IN THE SOFTWARE.
    */
   
    /*
    Parameters:
        $region - the Amazon(r) region (ca,com,co.uk,de,fr,jp)
        $params - an array of parameters, eg. array("Operation"=>"ItemLookup",
                        "ItemId"=>"B000X9FLKM", "ResponseGroup"=>"Small")
        $public_key - your "Access Key ID"
        $private_key - your "Secret Access Key"
    */

    // some paramters
    $method = "GET";
    $host = "ecs.amazonaws.".$region;
    $uri = "/onca/xml";
   
    // additional parameters
    $params["Service"] = "AWSECommerceService";
    $params["AWSAccessKeyId"] = $public_key;
    // GMT timestamp
    $params["Timestamp"] = gmdate("Y-m-d\TH:i:s\Z");
    // API version
    $params["Version"] = "2009-03-31";
   
    // sort the parameters
    ksort($params);
   
    // create the canonicalized query
    $canonicalized_query = array();
    foreach ($params as $param=>$value)
    {
        $param = str_replace("%7E", "~", rawurlencode($param));
        $value = str_replace("%7E", "~", rawurlencode($value));
        $canonicalized_query[] = $param."=".$value;
    }
    $canonicalized_query = implode("&", $canonicalized_query);
   
    // create the string to sign
    $string_to_sign = $method."\n".$host."\n".$uri."\n".$canonicalized_query;
   
    // calculate HMAC with SHA256 and base64-encoding
    $signature = base64_encode(hash_hmac("sha256", $string_to_sign, $private_key, True));
   
    // encode the signature for the request
    $signature = str_replace("%7E", "~", rawurlencode($signature));
   
    // create request
    $request = "http://".$host.$uri."?".$canonicalized_query."&Signature=".$signature;
   
    // do request
    $response = @file_get_contents($request);
    //$pxml = simplexml_load_string($response);
    return $response;

}
?>

Then in your php, do this :

<?php


$q = "KEYWORD HERE";

include("aws_signed_request.php");

$public_key = "PUBLIC KEY HERE";
$private_key = "PRIVATE KEY HERE";
$pxml = aws_signed_request("com", array("Operation"=>"ItemSearch","AssociateTag"=>"ASSOCIATE TAG HERE","Keywords"=>$q,"ResponseGroup"=>"Small","SearchIndex"=>"All"), $public_key, $private_key);


//print the entire xml
//print_r($pxml);


$root = new SimpleXMLElement($pxml);
$results = $root->Items->Item;

foreach($results as $result)
{
        echo "<a href='". $result->DetailPageURL ."'><br /><b>[".$result->ItemAttributes->ProductGroup."]</b> ".$result->ItemAttributes->Title."</a><br /><hr />";
}

?>




Installing NodeJS on Windows


Run this : 

@powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('http://bit.ly/psChocInstall'))"
Then this : 

cinst NodeJs

Test : 

Type

console.log("Hello World");

and save it in a .js file(say xxx.js). Type node xxx.js in cmd. Should print "Hello World".