我有这个方法:
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\r\nContent-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. \nString: $json\n" );
}
var_dump($result); exit;
return array ('json' => json_decode ( $result ), 'string' => $result, 'headers' => $headers );
}
如您所见,它创建了一个上下文并打开了一个流。但是,我有一个非常奇怪的错误。如果我发送此网址:
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
它会引发错误的请求错误,但如果我发送另一个错误:
http://sandbox.cxsearch.cxense.com/api/search/levelup?p_q=test&p_sm=idobject:desc&p_s=0&p_c=20&p_dr=title
它按预期工作。我必须对网址进行编码吗?
修复 了我能够弄清楚问题所在。我只需要更改 %20 的空间。仅此而已……