2015-11-18 17 views
6

我目前使用下面的代码奇怪的错误:内容长度未定义?使用雅虎的PlaceSpotter样本PHP代码

<?php 
/* Pre-requisite: Download the required PHP OAuth class from http://oauth.googlecode.com/svn/code/php/OAuth.php. This is used below */ 
require("OAuth.php"); 
$url = "https://yboss.yahooapis.com/geo/placespotter"; 
$cc_key = "MY_KEY"; 
$cc_secret = "MY_SECRET"; 
$text = "EYES ON LONDON Electric night in 100-meter dash"; 

$args = array(); 
$args["documentType"] = urlencode("text/plain"); 
$args["documentContent"] = urlencode($text); 

$consumer = new OAuthConsumer($cc_key, $cc_secret); 
$request = OAuthRequest::from_consumer_and_token($consumer, NULL,"POST", $url,$args); 
$request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, NULL); 
$url = sprintf("%s?%s", $url, OAuthUtil::build_http_query($args)); 
$ch = curl_init(); 
$headers = array($request->to_header());//.',Content-Length: '.strlen($text)); 

//print_r($headers.',Content-Length: '.strlen($text)); 

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
// somehow this line is not solving the issue 
// curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Length:'.strlen($text))); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
$rsp = curl_exec($ch); 
print_r($rsp); 
//echo "======= ENDING"; 
?> 

用我自己的访问密钥和所有与OAuth.php库。

不知何故我不断收到一个Content-Length未定义的错误。

如果我试图定义的Content-Length这样的(基于这里看到在计算器上一些答案:

curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Length:'.strlen($text))); 

我没有得到任何回应

可我知道怎么能这样问题得到解决

感谢

PS:?!php的例子来自官方的例子:https://gist.github.com/ydn/bcf8b301125c8ffa986f#file-placespotter-php


最新编辑

我根据@ alexblex的评论

<?php 
/* Pre-requisite: Download the required PHP OAuth class from http://oauth.googlecode.com/svn/code/php/OAuth.php. This is used below */ 
require("OAuth.php"); 
$url = "https://yboss.yahooapis.com/geo/placespotter"; 
$cc_key = "MY_KEY"; 
$cc_secret = "MY_SECRET"; 
$text = "EYES ON LONDON Electric from Singapore Raffles Place"; 
$args = array(); 
$args["documentType"] = urlencode("text/plain"); 
$args["documentContent"] = urlencode($text); 
$args["outputType"] = "json"; 
$consumer = new OAuthConsumer($cc_key, $cc_secret); 
$request = OAuthRequest::from_consumer_and_token($consumer, NULL,"PUT", $url, $args); 
$request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, NULL); 
$url = sprintf("%s?%s", $url, OAuthUtil::build_http_query($args)); 
$ch = curl_init(); 
$headers = array($request->to_header());//.',Content-Length: '.strlen($text)); 
//$headers = array($request->to_header().',Content-Length="'.strlen($text).'"'); 
//$headers = array($request->to_header().',Content-Length: 277'); 
print_r($headers); 
//print_r($headers.',Content-Length: '.strlen($text)); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $request->to_postdata()); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
$rsp = curl_exec($ch); 
echo "\n\n\n\n"; 
var_dump($rsp); 
//print_r($rsp); 
?> 

目前更新了我的代码,这个新的代码返回

{ “bossresponse”: {“responsecode”:“500”,“reason”:“来自后端的非200状态码 :415”}

错误。

+0

你能对错误信息更具体吗?请在这里复制粘贴您收到的错误消息。 –

+0

大家好,错误消息是:无内容长度 描述:无法处理此请求,因为没有指定Content-Length。 – DjangoRocks

+0

足够的信息? – DjangoRocks

回答

3

您不发送POST数据,因此没有发送Content-Length。要进行正确的卷曲请求,您需要指定要发送的数据。在你的情况可能是:

curl_setopt($ch, CURLOPT_POSTFIELDS, $request->to_postdata()); 

IF它应该是一个POST请求。 PlaceSpotter docs为:

PlaceSpotter Web服务仅支持HTTP PUT方法。其他HTTP方法不受支持。

所以,我认为它应该放在方法来代替:

$request = OAuthRequest::from_consumer_and_token($consumer, NULL,"PUT", $url,$args); 
.... 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); 

编辑415响应代码

这可能与双urlencode ING的问题。

尝试设置参数作为未编码的文本:

$args["documentType"] = "text/plain"; 
$args["documentContent"] = $text; 
+0

嗨,美好的一天,我试着按照您的代码示例使用PUT,但仍然收到'No Content Length Description:无法处理此请求,因为没有指定Content-Length.'错误。 – DjangoRocks

+0

@DjangoRocks,'PUT'请求仍然需要发送一些数据。 'CURLOPT_POSTFIELDS'仍然适用于'PUT'方法。 –

+0

嗨,我已经更新了我的代码。但是我现在收到'{“bossresponse”:{“responsecode”:“500”,“reason”:“来自后端的非200状态代码:415”}错误。我的代码错了?干杯。 – DjangoRocks

2

作为每RFC-2616Content-Type报头指示实体主体的大小而不头。所以如果你想在没有entity-body的情况下发出POST请求,你应该指定Content-Length: 0。试试这个。