duanjing1276 2013-07-08 14:31
浏览 64
已采纳

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.

  • 写回答

1条回答 默认 最新

  • duannai5858 2013-07-08 14:36
    关注

    I think you mean

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

    or if you want all of the $_GET parameters

    $queryString = http_build_query($_GET);
    

    replacement for your last code snippet should be

    if (isset($_GET['keyword'])) {
        $queryString = http_build_query($_GET);
    } else {
        echo 'Wrong!';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)