CURL "Required Content Length" error ... 3 days of searching, no luck

Before asking: I already checked every similar question that has already received an answer, and none of the proposed solutions work. So I hope someone can spot an error in my code.

When sending a cURL message to Google, I return with error 411, "POST requests require Content-length header"

//Info required to authenticate
$URL = "https://www.google.com/accounts/ClientLogin";
$POST = http_build_query(array(
 'Email' => 'XXXXXXX@gmail.com',
 'Passwd' => 'XXXXXXXXXXXXXXX',
 'source' => 'primary',
 'service' => 'cl'
));

$ch = curl_init( $URL );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POST);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$response = curl_exec($ch); //returns SID=<sid code>nLSID=<lsid code>nAuth=<auth code> or ERROR=<message>
if ( curl_errno($ch) )
 die( 'Error contacting server' );

//Successful auth results in http code 200
if ( curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200 )
 die( 'Failed to authenticate' );

//Extract auth code - Authorization: GoogleLogin auth=yourAuthToken
$auth_code = substr($response, strpos($response, 'Auth=')+5);

//We're done here
curl_close($ch);


$url = "https://www.googleapis.com/calendar/v3/calendars/".urlencode('XXXXXXXXXXXXX@developer.gserviceaccount.com')."/events?sendNotifications=true&pp=1&key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx";  

$post_data = http_build_query(array(
    "end" => array("dateTime" => "2013-14-11T10:40:00.000-07:00"),  
    "start" => array("dateTime" => "2013-14-11T10:00:00.000-07:00"),  
    "summary" => "my_summary",
    "description" => "my_description"
));

$headers = array(
    'Authorization: GoogleLogin auth='.$auth_code.'',
    'Content-Type: application/json'
);

$ch2 = curl_init();  
curl_setopt($ch2, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch2, CURLOPT_POSTFIELDS, $post_data);  

$output = curl_exec($ch2);  

curl_close($ch2);

echo '<pre>'.print_r($output).'</pre>';

Things I tried:

-Add 'Content-length:'. Strlen ($ post_data)

-Content-type 'x-www-form-urlencoded'

- using a very simple json string for post_data, so I did not use http_build_query

-Try to send it as PUT instead of POST

- And a few more things over the past few days that I can’t remember now

: MY-, PHP , . php-, ( AJAX)

. Wordpress CMS

-Kyle

+4
4

, CURLOPT_HTTPHEADER. json.

$post_data = array(
    "end" => array("dateTime" => "2013-14-11T10:40:00.000-07:00"),  
    "start" => array("dateTime" => "2013-14-11T10:00:00.000-07:00"),  
    "summary" => "my_summary",
    "description" => "my_description"
);

$post_data = json_encode($post_data);
curl_setopt($ch2, CURLOPT_HTTPHEADER, array(  
   'Content-Type: application/json',  
   'Content-Length: ' . strlen($post_data),)  
 );  
+4

...

curl , , ...

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch2, CURLOPT_POST, true);

... . ?

curl_setopt($ch,CURLOPT_HTTPHEADER,array('HeaderName: HeaderValue'));

1

curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-length:'.strlen($post_data)));
+1

, curl_getinfo, , ( , )... , , . Content-length, , . , :

, content_type= text/html... application/json

:

curl_setopt($ch2, CURLOPT_HTTPHEADER, array(
   'Authorization: GoogleLogin auth='.$auth_code,
   'Content-Type: application/json',  
   'Content-length:' . strlen($post_data))
 );

curl_getinfo:

Array (
    [url] => https://www.googleapis.com/calendar/v3/calendars/XXXXXXXXXX%40developer.gserviceaccount.com/events?sendNotifications=true&pp=1&key=XXXXXXXX-XXXXXXXXXXXXXXXXXXXXXX
    [content_type] => text/html; charset=UTF-8
    [http_code] => 411
    [header_size] => 147
    [request_size] => 737
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.104391
    [namelookup_time] => 0.000687
    [connect_time] => 0.025284
    [pretransfer_time] => 0.079614
    [size_upload] => 159
    [size_download] => 934
    [speed_download] => 8947
    [speed_upload] => 1523
    [download_content_length] => 934
    [upload_content_length] => 159
    [starttransfer_time] => 0.104356
    [redirect_time] => 0
    [certinfo] => Array
        (
        )

    [primary_ip] => 2607:f8b0:400e:c04::5f
    [primary_port] => 443
    [local_ip] => 2600:3c01::f03c:91ff:fe69:4a05
    [local_port] => 57581
    [redirect_url] =>  )
+1

, , , , , .

I followed this up using curl_setopt($ch, CURLINFO_HEADER_OUT, true);, which does curl_getinfo()include request headers in my output.

Using trim()for the variable fixed by it.

+1
source

All Articles