-3

我正在开发 Bing Search API:代码如下:

<?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>';
}
?>

我在浏览器上输入:

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

但是它给了我搜索结果的列表 50 项,完成没有错误,但是当我使用时:

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

它给了我Trying to get property of non-object,,,undefined offset反复。然后我意识到问题出在查询(关键字)上。该代码无法处理其中有空格的关键字。所以我尝试了这个:

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

我用它测试了http://localhost/MSP/SignIn/cariBing.php?keyword=statistical+terms 它给了我结果,但结果是指查询“关键字”,而不是我想要的“统计术语”,或者我输入的任何内容。我错过了哪里?首先十分感谢。

4

1 回答 1

0

我想你的意思是

$queryString = http_build_query(array("keyword"=>$_GET["keyword"]));

或者如果你想要所有的 $_GET 参数

$queryString = http_build_query($_GET);

替换您的最后一个代码段应该是

if (isset($_GET['keyword'])) {
    $queryString = http_build_query($_GET);
} else {
    echo 'Wrong!';
}
于 2013-07-08T14:36:32.757 回答