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 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch