Search Results working on localhost but not on my live site.

Answered Search Results working on localhost but not on my live site.

  • Friday, July 27, 2012 11:41 AM
     
      Has Code

    HI,

    Im using php example to get search results. Search results are fetching fine on localhost but not when i publish code to my online server.

    My Code is:

    <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)
    		    'header'  => "Authorization: Basic " . base64_encode("ignored:".$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>

All Replies

  • Friday, July 27, 2012 5:08 PM
     
      Has Code

    You don't say what error you're getting, if any. Certain errors in this scenario simply get swallowed. Try enabling the fetching of content even on failure status codes.

    $data = array(
      'http' => array(
        // 'proxy' => 'tcp://127.0.0.1:8888',
        'request_fulluri' => true,
        'ignore_errors' => true, // try adding this
        'header'  => "Authorization: Basic $encodedKey")
     );

    Maybe that will help you figure things out.

    ignore_errors added in PHP 5.2.10

    • Edited by VictorDavid Friday, July 27, 2012 5:29 PM
    •  
  • Monday, July 30, 2012 12:03 PM
     
      Has Code

    Hi,

    Thanks for reply. When i print object it print just 1. But on localhost it give me complete array data.

    $jsonobj = json_decode($response);
    
    echo '<pre>';
    echo print_r($jsonobj->d->results);
    echo '</pre>';

     Output is only 1:

  • Monday, July 30, 2012 5:43 PM
     
     Answered Has Code

    Try using print_r on the response itself, not the object decoded from json.

    print_r($response);

    The response is not JSON as it should be because of some error and this might help you find out what that error is.