API Integration Using Php code

PHP has a powerful JSON(Api Format) parsing mechanism, which, because PHP is a dynamic language, enables PHP developers to program against a JSON object graph in a very straightforward way. Example 9 shows a PHP page that sends a request to the JSON interface using the file_get_contents API to call the JSON endpoint, and the json_decode function to turn the results into an object graph that can be walked and turned into HTML.

Example 9: Using the API with PHP

   <link href="styles.css" rel="stylesheet" type="text/css" />
 <title>PHP Bing</title>
<body><form method="post" action="<?php echo $PHP_SELF;?>"> 
 Type in a search:<input type="text" id="searchText" name="searchText" value="<?php 
 if (isset($_POST['searchText'])){
  echo($_POST['searchText']); }
  else { echo('ashish');}
        <input type="submit" value="Search!" name="submit" id="searchButton" />
if (isset($_POST['submit'])) {
$request = 'http://api.search.live.net/json.aspx?Appid=<YourAppIDHere>&sources=image&query=' . urlencode( $_POST["searchText"]);
$response  = file_get_contents($request);
$jsonobj  = json_decode($response);
echo('<ul ID="resultList">');                    
foreach($jsonobj->SearchResponse->Image->Results as $value)
echo('<li class="resultlistitem"><a href="' . $value->Url . '">');
echo('<img src="' . $value->Thumbnail->Url. '"></li>');

} ?>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s