douyi9447 2012-05-18 18:18
浏览 97
已采纳

HTTP Stream PHP出错

I have this method:

protected function _sendRequest($url, $method, Busca_Cxense_Data $data, $get = null) {
    if (! isset ( $this->_urls [$url] )) {
        throw new Busca_Cxense_Exception_Argument ( "El tipo de url enviado no es valido. (type: {$url})" );
    }
    $url = $this->_urls [$url] . $data->getUrlKey () . ($get ? "$get" : '');
    $httpConfig = array ('http' => array ('method' => $method, 'request_fulluri' => $url, 'ignore_errors' => false ) );
    if ($data->getSendJson ()) {
        $json = $this->_setJson ( $data );
        $header = "Content-Type: application/json
Content-Length: " . strlen ( $json );
        $httpConfig ['http'] ['content'] = $json;
    } else {
        $header = "Content-Type: text/html";
    }
    $httpConfig ['http'] ['header'] = $header;
    $context = stream_context_create ( $httpConfig );
    $stream = fopen ($url, 'r', false, $context);
    $result = stream_get_contents($stream);
    $headers = stream_get_meta_data($stream);
    fclose($stream);
    if (! $result) {
        print_r ( $data );
        var_dump ( $url );
        print_r ( $httpConfig );
        throw new Busca_Cxense_Exception_MethodCall ( "Bad call. 
String: $json
" );
    }
    var_dump($result); exit;
    return array ('json' => json_decode ( $result ), 'string' => $result, 'headers' => $headers );
}

As you can see, it create a context and open an stream. However, I have a error very strange. If I send this url:

http://sandbox.cxsearch.cxense.com/api/search/levelup?p_aq=query%28category^1:%22preview%20trailer%22,token-op=or%29&p_sm=idobject:desc&p_s=0&p_c=20&p_dr=title

it throws a bad request error, but if I send this other one:

http://sandbox.cxsearch.cxense.com/api/search/levelup?p_q=test&p_sm=idobject:desc&p_s=0&p_c=20&p_dr=title

it works as expected. Do I have to encode the url or something?

FIXED I was able to figure what the problem was. I Only need to change the space for %20. And that was all...

  • 写回答

2条回答 默认 最新

  • dongyao4003 2012-05-21 18:59
    关注

    I was able to figure what the problem was. I Only need to change the space for %20. And that was all...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大