php file_get_content发布请求中的内容长度

时间:2014-01-03 07:36:42

标签: php google-data-api

我正在尝试通过php帖子请求更新Google fussion表。网址在Auth PlayGround上完全正常。见下图。抱歉!堆栈溢出不允许我发布图片,请使用此链接Image

但是当我使用php post request file_get_content尝试相同时,它会发出错误“411 Length Required”。代码需要多长时间。显然它不是内容长度,因为在这种情况下它的“零”。

<?php
$url = https://www.googleapis.com/fusiontables/v1/query?sql=INSERT INTO 1f_Z_********bQ-br8g17rFWBknri03fz-EQc (Name, Phone) VALUES ('Anees Hameed', '9895435751')
$Post = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/json',
        'header'  => 'GData Version: 3.0',
        'header'  => 'Authorization: Bearer '.$_Session[access_token]
    )
);
$Post= stream_context_create($Post);
$request = file_get_content($url, false, $Post);
?>

如何摆脱这个错误。

1 个答案:

答案 0 :(得分:0)

首先,代码中的URL是什么?!并且你没有用引号括起来。

  

411错误

     

这种错误在大多数网络流量中很少发生,特别是在   客户端系统是一个Web浏览器。这个问题只能通过解决   检查你的客户端系统正在尝试做什么,然后讨论   您的ISP为什么Web服务器需要'Content-Length'规范。

和你的代码:

<?php
$url = "https://www.googleapis.com/";
$content = "this is something to be sent to the server";
$Post = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/json',
        'header'  => 'GData Version: 3.0',
        'header'  => 'Authorization: Bearer '.$_Session[access_token],
        'header'  => 'Content-Length: '.strlen($content)
    )
);
$Post= stream_context_create($Post);
$request = file_get_content($url, false, $Post);
?>