none
Still problem with Basic authorization on Search API from PHP example

    Question

  • Hi,

    I looked through all related topics, covering my problem, and still have not find any decision resolving it:

    the PHP example from migration guide does not work neither in its initial state nor after all possible modifications proposed from various sources.

    Now in details:

    I have got an account key (for Search API, web results only) and checked it on my data page sending test requests. Everything works. Also I checked it putting the request URL directly in browser. Also worked. So account key is correct and it can be excluded from possible problems.

    Then I tried to use the example PHP code from migration guide, I have got nothing.

    Here is a part of the script:

    ---------------------------------------------------------------

    $accountKey = '(here is my working accountKey=)'; 

    $ServiceRootURL =  'https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/v1/';

    $WebSearchURL = $ServiceRootURL . 'Web?$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"] . '\'');                   

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

    var_dump($response);                    

    $jsonobj = json_decode($response);

    ----------------------------------------------

    As result $response returned "bool(false)".

    Next step was correction of the above code with "working" decisions from related topics:

    ----------------------------------------------------------------------------------

    $context = stream_context_create(array(     'http' => array(

                                'header'  => "Authorization: Basic " . base64_encode("ignored:" . $accountKey)

                            )

                        ));

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

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

                      var_dump($response);

    Now $respone returned "string(91) "The authorization type you provided is not supported. Only Basic and OAuth are supported". Cooool! At least some new stuff. But I still have no results returned.  Further there were day long dances around the script with all the possible corrections and “working” decisions. As you probably understood result was the same.

    Now could you please let me know how I can migrate from my ideally working Bing Search  API 2.0 in my PHP scripts to the enhanced and very comfortable new one? I would be extremely thankful if you provide me with REALLY working PHP example. For two days I have examined all the possible forums on the web and related topics, so please do not send me to any "existing decisions" - they do not work for me.


    Saturday, July 28, 2012 10:18 AM

Answers

  • I just found this example: try out hpe it works!

    <?
    $accountKey = 'your Azure key here';
    $q = $_GET['q'];
    if (get_magic_quotes_gpc()) $q = stripslashes($q);
    // note that $q is unsafe!
    ?>

    <form method="get" action="">
    <input name="q" type="text" value="<?=htmlentities($q)?>" />
    <input type="submit" value="Search" />
    </form>


    <?
    function sitesearch ($query, $site, $accountKey, $count=NULL){
    // code from http://go.microsoft.com/fwlink/?LinkID=248077
    $ServiceRootURL = 'https://api.datamarket.azure.com/Bing/Search/';
    $WebSearchURL = $ServiceRootURL . 'Web?$format=json&Query=';
    $context = stream_context_create(array(
    'http' => array(
    'request_fulluri' => true,
    'header' => "Authorization: Basic " . base64_encode($accountKey . ":" . $accountKey)
    )
    ));
    $request = $WebSearchURL . urlencode("'$query site:$site'"); // note the extra single quotes
    if ($count) $request .= "&\$top=$count"; // note the dollar sign before $top--it's not a variable!
    return json_decode(file_get_contents($request, 0, $context));
    }

    function showresponse ($q, $next, $results){
    $count = count($results);
    if ($count==0){
    $resultwords = 'No results';
    }else if ($next){
    $resultwords = "More than $count results";
    }else if ($count == 1){
    $resultwords = '1 result';
    }else{
    $resultwords = "$count results";
    }
    echo "<p>$resultwords found for <strong>".htmlentities($q)."</strong></p>\n";
    if ($count == 0) return;
    echo "<dl>\n";
    foreach ($results as $result) echo "<dt><a href='{$result->Url}'>{$result->Title}</a></dt>\n<dd>{$result->Description}</dd>\n";
    echo "</dl>\n";
    }

    if ($q){
    // get search results
    $result = sitesearch ($q, $_SERVER['HTTP_HOST'], $accountKey, 10);
    showresponse($q, $result->d->{__next}, $result->d->results);
    $searchURL = 'http://www.bing.com/search?q='.urlencode("$q site:{$_SERVER['HTTP_HOST']}");
    echo "<p><a href='$searchURL'>See all results</a><p>\n";
    }
    <


    Murali

    Monday, July 30, 2012 1:21 PM
  • Thanks guys. There are quite a few ways to make PHP work with the Marketplace.

    One of the other ways is to use OData PHP library: http://odataphp.codeplex.com/

    It looks like current sample in the migration guide breaks for quite a few people, so we would need to update it. My current assumption is that most breaks are resulting from the proxy setting in the sample. We'll take a look and try to fix.

    Monday, July 30, 2012 4:07 PM
    Owner

All replies

  • I'm in pretty much the exact same situation.  I got the additional details on my error "[function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 401 The authorization type you provided is not supported. Only Basic and OAuth are supported"

    Help would be great.

    Saturday, July 28, 2012 1:25 PM
  • I got mine fixed although unfortunately I have no idea what was the original problem because as far as I can tell the code is functionally identical.

    What I did was started from scratch again using the migration guides code:

    <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 = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=';
                
                        $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);
                        
                        $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 of course added my key.  In addition I commented out the proxy and request_fulluri parts of the stream_context_create function call.  This has been suggested elsewhere and seems to have solved some peoples problems.  Last I changed the search strings so that the $ were inserted as individual blocks .'$'. because my server tried to interpret them as variables and replaced the $ and the following word with a blanks space as though it was an unset variable.

    I had done all of these things before starting over so I'm not sure what exactly was off that made this work but my first try not, still hopefully this can be of help to someone.

    Sunday, July 29, 2012 1:03 PM
  • Thanks, did you post your resulting code or initial sample?

    I am looking at your sample and not seeing any difference with the standard one we post in Search API migration guide.

    The first code snippet from this thread had authentication encoding line changed and this seemed to result in an error.

    Removing proxy is helpful indeed, the current sample assumes there might be a need for the proxy and you would put your own proxy address instead of the localhost:8888. If you do not require proxy, you can just comment it out.

    Thanks,

    Max

    Monday, July 30, 2012 6:06 AM
    Owner
  • "The first code snippet from this thread had authentication encoding line changed and this seemed to result in an error." - a very helpful notice)) If you mean this one: 

    'header'  => "Authorization: Basic " . base64_encode($accountKey":" . $accountKey))));                   it is just my technical mistake while pasting the block to the thread. As I told, I had started from the very initial untouched script from the migration guide. And the line was: 'header'  => "Authorization: Basic " . base64_encode($accountKey. ":" . $accountKey)))); and did not work either.

    Just accept the fact that the sample does not work properly for many people and try to correct it.

    By the way, in my case the problem was resolved by using next script instead of Microsoft's example:

       $accountKey = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=';
        $ServiceRootURL =  'https://api.datamarket.azure.com/Bing/SearchWeb/';  
        $WebSearchURL = $ServiceRootURL . 'Web?$format=json&Query=';
    
        $request = $WebSearchURL . urlencode( '\'' . $_POST["searchText"] . '\'');
    
        $process = curl_init($request);
        curl_setopt($process, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($process, CURLOPT_USERPWD,  $accountKey . ":" . $accountKey);
        curl_setopt($process, CURLOPT_TIMEOUT, 30);
        curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
        $response = curl_exec($process);
    
        $jsonobj = json_decode($response);
    

    But it has no connection to the Microsotf support team. It was found at stackoverflow.com (http://stackoverflow.com/questions/10844463/bing-search-api-and-azure).

    • Proposed as answer by Ken Gibson Tuesday, September 11, 2012 7:38 PM
    Monday, July 30, 2012 8:14 AM
  • I just found this example: try out hpe it works!

    <?
    $accountKey = 'your Azure key here';
    $q = $_GET['q'];
    if (get_magic_quotes_gpc()) $q = stripslashes($q);
    // note that $q is unsafe!
    ?>

    <form method="get" action="">
    <input name="q" type="text" value="<?=htmlentities($q)?>" />
    <input type="submit" value="Search" />
    </form>


    <?
    function sitesearch ($query, $site, $accountKey, $count=NULL){
    // code from http://go.microsoft.com/fwlink/?LinkID=248077
    $ServiceRootURL = 'https://api.datamarket.azure.com/Bing/Search/';
    $WebSearchURL = $ServiceRootURL . 'Web?$format=json&Query=';
    $context = stream_context_create(array(
    'http' => array(
    'request_fulluri' => true,
    'header' => "Authorization: Basic " . base64_encode($accountKey . ":" . $accountKey)
    )
    ));
    $request = $WebSearchURL . urlencode("'$query site:$site'"); // note the extra single quotes
    if ($count) $request .= "&\$top=$count"; // note the dollar sign before $top--it's not a variable!
    return json_decode(file_get_contents($request, 0, $context));
    }

    function showresponse ($q, $next, $results){
    $count = count($results);
    if ($count==0){
    $resultwords = 'No results';
    }else if ($next){
    $resultwords = "More than $count results";
    }else if ($count == 1){
    $resultwords = '1 result';
    }else{
    $resultwords = "$count results";
    }
    echo "<p>$resultwords found for <strong>".htmlentities($q)."</strong></p>\n";
    if ($count == 0) return;
    echo "<dl>\n";
    foreach ($results as $result) echo "<dt><a href='{$result->Url}'>{$result->Title}</a></dt>\n<dd>{$result->Description}</dd>\n";
    echo "</dl>\n";
    }

    if ($q){
    // get search results
    $result = sitesearch ($q, $_SERVER['HTTP_HOST'], $accountKey, 10);
    showresponse($q, $result->d->{__next}, $result->d->results);
    $searchURL = 'http://www.bing.com/search?q='.urlencode("$q site:{$_SERVER['HTTP_HOST']}");
    echo "<p><a href='$searchURL'>See all results</a><p>\n";
    }
    <


    Murali

    Monday, July 30, 2012 1:21 PM
  • Thanks guys. There are quite a few ways to make PHP work with the Marketplace.

    One of the other ways is to use OData PHP library: http://odataphp.codeplex.com/

    It looks like current sample in the migration guide breaks for quite a few people, so we would need to update it. My current assumption is that most breaks are resulting from the proxy setting in the sample. We'll take a look and try to fix.

    Monday, July 30, 2012 4:07 PM
    Owner
  • As with many PHP samples, there can be some things you might need to change - depending on your PHP version / setup.

    For OP: I was wondering which version of PHP you are using? The ability to pass context was added in PHP 5. It's a long shot, I think most installations have upgraded, but just in case. 


    Monday, July 30, 2012 4:59 PM
  • We updated the sample to remove proxy settings.

    Should be working now.

    Thanks,

    Max

    Thursday, August 02, 2012 11:21 PM
    Owner
  • Could someone please post a complete working web example.  T

    Thanks

    TD

    • Proposed as answer by febrookbee Friday, August 24, 2012 7:16 AM
    • Unproposed as answer by febrookbee Friday, August 24, 2012 7:16 AM
    Friday, August 17, 2012 8:09 PM
  • This makes the code to work:  xhr.setRequestHeader('Authorization', 'Basic '+encodedkey);

      var wshost = 'https://api.datamarket.azure.com/Bing/SearchWeb/Web';

      var wssearchparam=query and other attributes for search!

     //send request to bing api
        $.ajax({
                type: 'GET',
                    url: wshost,
                    data: wssearchparam,
                    dataType: "json",
                    context: this,
                    beforeSend: function(xhr){
                    xhr.setRequestHeader('Authorization', 'Basic '+encodedkey);
                },
                    success: function(data){
                    searchResults = data.d.results;
                    alert(searchResults);
                    alert(searchResults.length);
                    for (var i = 0; i < searchResults.length; i++) {
                    var object = searchResults[i];
                    for (property in object) {
                    var value = object[property];
                    alert(property + "=" + value);
                     }
                    }
                }
            });

    Try to set up a proxy call, it will work in IE also!.


    Murali

    • Proposed as answer by febrookbee Friday, August 24, 2012 7:17 AM
    Friday, August 24, 2012 7:17 AM
  • What a nightmare.  Dout Des has it right. 
    • Edited by AntiochIst Monday, September 03, 2012 9:26 PM
    Monday, September 03, 2012 9:15 PM
  • Thanks - the above script (using curl) fixed this issue for me.
    Tuesday, September 11, 2012 7:38 PM
  • Hello Max,

    Sorry to spoil the fun --- but this problem isn't solved.

    I have tried straight PHP and also with CURL, and via 2 hosting companies, and I still get the venerable error:

    "The authorization type you provided is not supported. Only Basic and OAuth are supported"

    Here are snippets:

    $auth = base64_encode($userName.":".$accountKey);
    $data = array(
      'http' => array(
         'header'  => 'Authorization: Basic ' . $auth
      )
    );
    $context = stream_context_create($data);
    ini_set("allow_url_fopen", 1); //checked out - it is allowed
    $response = file_get_contents($requestUri, 0, $context);

    or alternatively with CURL:

    $auth = base64_encode($userName.":".$accountKey);
    $process = curl_init($requestUri);
     curl_setopt($process, CURLOPT_SSL_VERIFYHOST, 0);
     curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);  
         curl_setopt($process, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
         curl_setopt($process, CURLOPT_USERPWD,  $accountKey . ":" . $accountKey);
         curl_setopt($process, CURLOPT_TIMEOUT, 30);
         curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
    $response = curl_exec($process);

    Either way, either hosting company --- same error. My hosts could not find a reason on their end.

    This bug is very prevalent.

    Please find a solution.

    Thanks,

    Barry F.

    Sunday, April 14, 2013 1:07 PM
  • I do so much that is not feasible ???
    Sunday, April 14, 2013 5:50 PM
  • Hi Barry this works for me on dreamhost but it takes like forever for the bing results to load. I wish I could solve this issue!

    Note regarding the var, $mfg: This is a manufactuer name passed from another page on site

    <?php 
    $mfg =  trim($_REQUEST['mfg']); 
    $accountKey = 'accountkeyhere';

    $ServiceRootURL =  'https://api.datamarket.azure.com/Bing/Search/';
    $WebSearchURL = $ServiceRootURL . 'Web?$format=json&$top=8&Query=';
    $context = stream_context_create(array(
        'http' => array(
        'request_fulluri' => true,
        'header'  => "Authorization: Basic " . base64_encode($accountKey . ":" . $accountKey)
        )
    ));
    $request = $WebSearchURL .urlencode( '\'' .$mfg."  distributors" . '\'');
    $response = file_get_contents($request, 0, $context);
    print("<!--".$response."-->");
    $jsonobj = json_decode($response);
    ?>

    The HTML: (Excuse the old school syntax please)

              <?php foreach($jsonobj->d->results as $value) { ?>
              <div class="mfg-sup">
                <h3>
                  <a href="<?php echo $value->Url ?>" rel="nofollow" target="_blank">
                    <?php echo $value->Title ?>
                  </a>
                </h3>
                <p><?php echo $value->Description ?></p>
                <p class="url-txt"><?php echo $value->Url ?></p>
              </div>
              <?php } ?>

    Thursday, April 25, 2013 3:17 AM
  • I had the same problem and solved it by adding the extension php openssl 
    Thursday, May 30, 2013 12:30 AM
  • I had the same problem. Spent a week solving.

    Finally, I added

    curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);

    to the CURL version of the code and it worked.

    So, if nothing helps try

    $process = curl_init($request);
        curl_setopt($process, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($process, CURLOPT_USERPWD,  $accountKey . ":" . $accountKey);
        curl_setopt($process, CURLOPT_TIMEOUT, 30);
        curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
    	curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
        $response = curl_exec($process);

    Saturday, June 22, 2013 4:43 AM