http_build_query()无法检索所需的查询,PHP

I'm working on Bing Search API: Here's the code:

<?php
if (isset($_GET['keyword'])) {
$keyword = $_GET['keyword'];
} else {
  echo 'Wrong!';
}
$key = 'NNNNN'; //key for API
$root = 'https://api.datamarket.azure.com/Bing/Search/';
$search = $root . 'Web?$format=json&Query=';
$req = $search . '\'' . $keyword . '\'';
$ch = curl_init($req);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $key . ":" . $key);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($ch);
$json = json_decode($resp);
foreach ($json->d->results as $item) {
$rss_item = array(
    'Title' => $item->Title,
    'Description' => $item->Description,
    'DisplayUrl' => $item->DisplayUrl,
    'Url' => $item->Url,
);
array_push($desArray, $item->Description);
array_push($rss_array, $rss_item);
}
for ($i = 0; $i < 50; $i++) {
    echo '<p class="bagi">' . '<a href="' . $rss_array[$i]['Url'] . '">' . $rss_array[$i]['Title'] . '</a>
        <a href="' . $rss_array [$i]['Url'] . '" target="_blank">' . '<img src="'.base_url().'TAMPILAN/images/open_new_tab.jpg" width="10px" height="10px" title="Open in new tab"></a>
            <br/>' .
    $rss_array [$i]['Description'] . '</br>' .
    '<hr/>' .
    '</p>';
}
?>

I typed on my browser with:

http://localhost/MSP/SignIn/cariBing.php?keyword=statistics

But it gave me the list of the search results 50 items, complete no error, but when I used:

http://localhost/MSP/SignIn/cariBing.php?keyword=statistical+terms

It gave me Trying to get property of non-object, undefined offset, repeatedly. Then I realised the problem was on the query (keyword). The code couldn't handle such keyword that had space in it. So I tried this:

if (isset($_GET['keyword'])) {
$queryString = array();
foreach ($_GET as $keyword => $value) {
    $queryString[] = $keyword .'='. $value;
}
$queryString = http_build_query($queryString, $keyword);
} else {
echo 'Wrong!';
}

I tested it with the http://localhost/MSP/SignIn/cariBing.php?keyword=statistical+terms It gave me the results, but the results refers to a query "keyword", not "statistical terms" as I wanted, or whatever I typed. Where did I miss? Thanks very much in advance.

查看全部
duanjing1276
duanjing1276
2013/07/08 14:31
  • php
  • forms
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复