2

像这样的带有 curl 的查询可以正常工作:

curl -XPOST -H "Content-Type: application/json" -d '{"query":"porges","start":0,"rows":10,"lang":"ENGLISH"}' http://localhost:8080/services/rest/index/z56508/search/field/search

在我的情况下,我在那里获得了 11 次点击。但是,当我尝试将其转换为 HTTP_Request2 时,调用会返回数据库中的所有命中。

我查看了Http_Request2 POST 请求无法在此处编写代码:

    require_once 'HTTP/Request2.php';
    $url = "http://localhost:8080/services/rest/index/z56508/search/field/search";
    $data = array("query"=>"porges","start"=>"0","rows"=>"10","lang"=>"ENGLISH");

    $r = new HTTP_Request2($url);
    $r->setHeader('Content-Type', 'application/json;charset=UTF-8');
    $r->setMethod(HTTP_Request2::METHOD_POST)
            ->addPostParameter($data);
    $response = $r->send();
    $body = $response->getBody();
    echo $body;

我究竟做错了什么?似乎"query" => "porges"被忽略了,但为什么呢?

4

1 回答 1

3

阅读友好手册: http: //pear.php.net/manual/en/package.http.http-request2.request.php#package.http.http-request2.request.body

addPostParameter()当您想根据application/x-www-form-urlencodedmultipart/form-dataContent-Type 规则生成 POST 请求正文时应使用(提示:这不是您尝试发送的 JSON)。如果您有预先构建的请求正文,请使用setBody()

$request->setBody('{"query":"porges","start":0,"rows":10,"lang":"ENGLISH"}');
于 2014-12-08T09:52:05.270 回答