PHP Bing example failing with Connection Refused

Answered PHP Bing example failing with Connection Refused

  • Saturday, July 28, 2012 11:44 PM
     
      Has Code

    I've seen this kind of question before, but I have to ask again. As far as I can tell I'm subscribed to Bing (full, not Web Only) properly. Still, every attempt to run the example PHP script fails with Connection Refused.

    Putting the exact request (as echoed by the PHP script on the failed page) into my browser and supplying the same accountKey at the prompt returns a proper result.

    At the risk of redundancy I will reproduce the script here, in case there's something I'm still missing:

    <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 = 'BBBBBBB...=';
                
                        $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>

    Anything I'm doing wrong here?

    Thanks for any help

All Replies

  • Sunday, July 29, 2012 7:29 PM
     
     Answered Has Code

    This has been a problem for several people. Please remove (or comment out) the proxy configuration line in the stream context.

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

    Victor David