Content-Length undefined? Strange error using PHP code PHP PlaceSpotter

I am currently using the following code:

<?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"; ?> 

With my own access keys and all, with the OAuth.php library.

Somehow I kept getting Content-Length undefined error.

If I tried to define Content-Length like this (based on some answers that can be seen here in StackOverFlow:

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

I do not get an answer.

Can I find out how to solve this problem?

Thanks!

PS: php example comes from official example: https://gist.github.com/ydn/bcf8b301125c8ffa986f#file-placespotter-php


LAST CHANGE

I updated my code based on @alexblex comment

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

This new code is currently returning

{"bossresponse": {"responsecode": "500", "reason": "non-200 status code from backend: 415"}

error.

+6
source share
2 answers

You do not send POST data, therefore Content-Length is not sent. To make the correct curl request, you need to specify what data you want to send. In your case, this will most likely be:

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

IF , it must be a POST request. PlaceSpotter docs read:

PlaceSpotter Web Service only supports the HTTP PUT method. Other HTTP methods are not supported.

So, I guess this should be the PUT method:

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

EDIT for 415 response code

This may be a problem with double urlencode ing.

Try setting the arguments as uncoded text:

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

In accordance with RFC-2616, the Content-Type header indicates the size of the object body without headers. Therefore, if you want to make POST requests without an entity, you must specify Content-Length: 0 . Try it.

+2
source

All Articles