内容长度未定义?使用Yahoo的PlaceSpotter示例php代码的奇怪错误

Dja*_*cks 6 php yahoo-api

我目前正在使用以下代码:

<?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";
?>
Run Code Online (Sandbox Code Playgroud)

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

不知怎的,我不断得到Content-Length未定义的错误.

如果我试图像这样定义Content-Length(基于StackOverFlow上的一些答案:

curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Length:'.strlen($text)));
Run Code Online (Sandbox Code Playgroud)

我没有得到任何回应.

我可以知道如何解决这个问题?

谢谢!

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);
?>
Run Code Online (Sandbox Code Playgroud)

目前,这个新代码返回一个

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

错误.

Ale*_*lex 3

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

curl_setopt($ch, CURLOPT_POSTFIELDS, $request->to_postdata());
Run Code Online (Sandbox Code Playgroud)

如果它应该是 POST 请求。PlaceSpotter文档中写道:

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

所以我认为它应该是 PUT 方法:

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

编辑 415 响应代码

这可能是 double ing 的问题urlencode

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

$args["documentType"] = "text/plain";
$args["documentContent"] = $text;
Run Code Online (Sandbox Code Playgroud)