Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New Bing API PHP example doesnt work

Tags:

rest

php

bing

Microsoft's own PHP example for new Bing API doesn't work. I tried in many ways, it just shows:

Server Error
401 - Unauthorized: Access is denied due to invalid credentials.
You do not have permission to view this directory or page using the credentials that you supplied.

Example Coding given in the official documentation is below, it breaks up at

'proxy' => 'tcp://127.0.0.1:8888',  

I am 100% sure my key is correct, and when I just enter it in the browser url it works fine, i.e

https://api.datamarket.azure.com/Bing/SearchWeb/Web?Query=%27love+message%27

(you need to put the API key as your password and username can be anything)

<html>
    <head>
        <link href="styles.css" rel="stylesheet" type="text/css" />
        <title>PHP Bing</title>
    </head>
    <body>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
            Type in a search:

            <input type="text" id="searchText" name="searchText"
                value="<?php
                        if (isset($_POST['searchText']))

                                   {
                            echo($_POST['searchText']);
                        }
                        else
                        {
                            echo('sushi');
                        }
                       ?>"
            />

            <input type="submit" value="Search!" name="submit" id="searchButton" />
            <?php
                if (isset($_POST['submit']))
                {
                    // Replace this value with your account key
                    $accountKey = 'BKqC2hIKr8foem2E1qiRvB5ttBQJK8objH8kZE/WJVs=';

                    $ServiceRootURL = 'https://api.datamarket.azure.com/Bing/Search/';

                    $WebSearchURL = $ServiceRootURL . 'Image?$format=json&Query=';

                    $context = stream_context_create(array(
                        'http' => array(
                            //'proxy' => 'tcp://127.0.0.1:8888',
                            'request_fulluri' => true,
                            'header' => "Authorization: Basic " . base64_encode($accountKey . ":" . $accountKey)
                        )
                    ));

                    $request = $WebSearchURL . urlencode( '\'' . $_POST["searchText"] . '\'');

                    echo($request);

                    $response = file_get_contents($request, 0, $context);

                    print_r($response);

                    $jsonobj = json_decode($response);

                    echo('<ul ID="resultList">');

                    foreach($jsonobj->d->results as $value)
                    {
                        echo('<li class="resultlistitem"><a href="' . $value->MediaURL . '">');

                        echo('<img src="' . $value->Thumbnail->MediaUrl. '"></li>');
                    }

                    echo("</ul>");
                }
            ?>
        </form>
    </body>
</html>

I have tried both google API and Yahoo API both, none of those were as difficult as this.

like image 382
mahen3d Avatar asked Jul 12 '12 11:07

mahen3d


3 Answers

after days of argument with microsoft techinchal support they accpeted that it didnt work

here is the proper coding which uses CURL do this in the BING API, apply CURL method instead of the file_get_contents which can’t pass the correct authentication information from Linux client to BING service.

<html>
    <head>
        <title>PHP Bing</title>
    </head>
    <body>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
            Type in a search:

            <input type="text" id="searchText" name="searchText"
                value="<?php
                        if (isset($_POST['searchText']))

                                   {
                            echo($_POST['searchText']);
                        }
                        else
                        {
                            echo('sushi');
                        }
                       ?>"
            />

            <input type="submit" value="Search!" name="submit" id="searchButton" />
            <?php


                if (isset($_POST['submit']))
                {

            $credentials = "username:xxx";

                $url= "https://api.datamarket.azure.com/Bing/SearchWeb/Web?Query=%27{keyword}%27";        
                $url=str_replace('{keyword}', urlencode($_POST["searchText"]), $url);
                $ch = curl_init();

            $headers = array(
                    "Authorization: Basic " . base64_encode($credentials)
                );

                $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
                curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
                curl_setopt($ch, CURLOPT_FAILONERROR, true);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                curl_setopt($ch, CURLOPT_AUTOREFERER, true);
                curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

                $rs = curl_exec($ch);
            echo($rs);
                curl_close($ch);
                return $rs;

        }

            ?>
        </form>
    </body>
</html>
like image 129
mahen3d Avatar answered Nov 14 '22 21:11

mahen3d


I had to add

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

in order to make it work, at least in my local copy (WAMP).

Hope it helps, I have been messing with this all the day.

like image 41
Hernan Avatar answered Nov 14 '22 21:11

Hernan


$WebSearchURL = $ServiceRootURL . 'Image?$format=json&Query=';  

This is part of the prob

This wont give the url bing is looking for

e.g. https://api.datamarket.azure.com/Bing/SearchWeb/Web?Query=%27love+message%27 

it would be

https://api.datamarket.azure.com/Bing/Search/Image?$format=json&Query=%27love+message%27 

whereas you want a web not an image search and also format and other parameters shld be after the query

"image" should be "web"

I just spent 3 days trying to get this to work.

like image 20
greta Avatar answered Nov 14 '22 22:11

greta