cURL'内容长度要求'错误... 3天搜索,没有运气

Kyl*_*yle 7 php curl google-calendar-api

在你提出问题之前:我已经检查过每个已经有答案的类似问题,并且所提出的解决方案都不起作用.所以我希望有人能够注意到我的代码中的错误.

在向Google提交cURL帖子时,我收到411错误,"POST请求需要内容长度标头"

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

我尝试过的事情:

- 添加'Content-length:'.strlen($ post_data)

- 内容类型'x-www-form-urlencoded'

- 为post_data使用一个非常简单的json字符串,这样我就不使用http_build_query了

- 尽量使其作为PUT而不是POST发送

- 在过去几天的其他一些事情,我现在还不记得了

意图:仅使用PHP将事件添加到仅我的日历,而不需要用户执行身份验证步骤.这必须能够在php函数中异步运行(通过AJAX调用)

注意:不使用Wordpress或任何其他CMS

-Kyle

Roo*_*dra 8

我想你需要设置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),)  
 );  
Run Code Online (Sandbox Code Playgroud)


Len*_*nny 1

更新了答案...

在第二个curl调用中,您似乎在一行中使用了错误的句柄......

curl_setopt($ch, CURLOPT_POST, true);
Run Code Online (Sandbox Code Playgroud)

应该是

curl_setopt($ch2, CURLOPT_POST, true);
Run Code Online (Sandbox Code Playgroud)

原始答案... 您没有传递标头。您是否尝试以这种格式将内容长度作为标头传递?

curl_setopt($ch,CURLOPT_HTTPHEADER,array('HeaderName: HeaderValue'));
Run Code Online (Sandbox Code Playgroud)

您的标头目前只是数字 1

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