2015-11-18 4 views
6

を使用して奇妙なエラー:OAuth.phpライブラリとContent-Lengthは未定義ですか?私は現在、次のコードを使用していYahooの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"; 
?> 
私自身のアクセスキーで

とすべて、。

どういうわけか私はContent-Lengthの定義されていないエラーが発生しました。

私はStackOverflowの上ここに見られるいくつかの回答に基づいて、この(のようなContent-Length定義しようとした場合:

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

を私は何の応答を得ることはありません

私はどのようにこのことができます知っているかもしれません。問題を解決することが

おかげ

PS:?!https://gist.github.com/ydn/bcf8b301125c8ffa986f#file-placespotter-php

012: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); 
?> 

に基づいて自分のコードを更新しました


LATEST EDIT

は現在、この新しいコードは

{ "bossresponse" を返します。 {"responsecode": "500"、 "reason": "バックエンドの非200ステータスコード :415"}

エラーです。

+0

エラーメッセージの詳細は?取得したエラーメッセージをここにコピーして貼り付けてください。 –

+0

こんにちは、良いメッセージです:No Content Length 説明:Content-Lengthが指定されていないため、この要求を処理できませんでした。 – DjangoRocks

+0

は十分な情報ですか? – DjangoRocks

答えて

3

POSTデータを送信しないため、Content-Lengthは送信されません。正しいカールリクエストを作成するには、送信するデータを指定する必要があります。それはPOSTリクエストでなければなりませんのIF

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

:あなたのケースでは、可能性が高いです。 PlaceSpotter docsは、次のとおりです。

PlaceSpotter Webサービスは、HTTP PUTメソッドのみをサポートしています。その他のHTTPメソッドはサポートされていません。

だから私は、それは代わりにメソッドを配置する必要がありますと仮定します。415レスポンスコード

ため

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

EDITそれは二重の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を指定する必要があります。これを試してみてください。