简体   繁体   English

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

[英]Content-length in php file_get_content post request

I am trying to update Google fussion table through a php post request. 我正在尝试通过php发布请求更新Google融合表。 URL works perfectly fine on Auth PlayGround . URL在Auth PlayGround上运行良好。 See the image below. 参见下图。 Sorry! 抱歉! Stack overflow is not allowing me to post image, please use this link Image 堆栈溢出不允许我发布图片,请使用此链接图片

But when I try the same using php post request file_get_content, it shoots with an error "411 Length Required". 但是,当我尝试使用php post request file_get_content尝试相同操作时,它会显示错误“ 411 Length Required”。 What length is the code asking for. 代码要求多少长度。 Obviously its not content length because its "Zero" in this case. 显然,它的内容长度不正确,因为在这种情况下为“零”。

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

How to get rid of this error. 如何摆脱这个错误。

First of all what's that URL in your code?! 首先,您的代码中的URL是什么?! and you didn't wrapped it within quotes either. 而且您也没有将其用引号引起来。

411 error 411错误

This error seldom occurs in most Web traffic, particularly when the client system is a Web browser. 大多数Web通信中很少发生此错误,尤其是当客户端系统是Web浏览器时。 The problem can only be resolved by examining what your client system is trying to do then discussing with your ISP why the Web server expects a 'Content-Length' specification. 只有通过检查您的客户端系统正在尝试做什么,然后与您的ISP讨论为什么Web服务器需要“ Content-Length”规范,才能解决该问题。

and your code: 和您的代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM