PHP - 通过 file_get_contents 发布 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11319520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - Posting JSON via file_get_contents
提问by Ben
I am trying to POST JSON content to a remote REST endpoint, however the 'content' value appears to be empty on delivery. All other headers etc are being received correctly, and the web service tests successfully with a browser based test client.
我正在尝试将 JSON 内容发布到远程 REST 端点,但是“内容”值在交付时似乎为空。所有其他标头等都被正确接收,并且 Web 服务使用基于浏览器的测试客户端成功测试。
Is there a problem with my syntax below where I specify the 'content' field?
我在下面指定“内容”字段的语法有问题吗?
$data = array("username" => "duser", "firstname" => "Demo", "surname" => "User", "email" => "[email protected]");
$data_string = json_encode($data);
$result = file_get_contents('http://test.com/api/user/create', null, stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => array('Content-Type: application/json'."\r\n"
. 'Authorization: username:key'."\r\n"
. 'Content-Length: ' . strlen($data_string) . "\r\n"),
'content' => $data_string)
)
));
echo $result;
采纳答案by Bob Davies
This is the code I always use and it looks pretty similar (though this is of course for x-www-form-urlencoded).
Perhaps your username:keyneeds to be base64_encode'd.
这是我一直使用的代码,它看起来非常相似(尽管这当然适用于 x-www-form-urlencoded)。也许你username:key需要被base64_encode'd。
function file_post_contents($url, $data, $username = null, $password = null)
{
$postdata = http_build_query($data);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
if($username && $password)
{
$opts['http']['header'] = ("Authorization: Basic " . base64_encode("$username:$password"));
}
$context = stream_context_create($opts);
return file_get_contents($url, false, $context);
}
回答by Dana Kolodziejczyk
The earlier response of
之前的回应
function file_post_contents($url, $data, $username = null, $password = null) {
$postdata = http_build_query($data);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
if($username && $password)
{
$opts['http']['header'] = ("Authorization: Basic " . base64_encode("$username:$password"));
}
$context = stream_context_create($opts);
return file_get_contents($url, false, $context);}
is incorrect. This function works sometimes, but it is inaccurate and will fail if you're not using the Content-type of application/x-www-form-urlencoded and you pass in a username and password.
是不正确的。此函数有时会起作用,但它不准确并且如果您未使用 application/x-www-form-urlencoded 的 Content-type 并且您传入用户名和密码,则会失败。
It's working for the writer because application/x-www-form-urlencoded is the default Content-type, but his handling of the username and password is overwriting the earlier declaration of content type.
它对作者有效,因为 application/x-www-form-urlencoded 是默认的内容类型,但他对用户名和密码的处理覆盖了内容类型的早期声明。
Here is the corrected function:
这是更正后的函数:
function file_post_contents($url, $data, $username = null, $password = null){
$postdata = http_build_query($data);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'content' => $postdata
)
);
if($username && $password)
{
$opts['http']['header'] .= ("Authorization: Basic " . base64_encode("$username:$password")); // .= to append to the header array element
}
$context = stream_context_create($opts);
return file_get_contents($url, false, $context);}
Note the line: $opts['http']['header' .= (dot equals to append to the array element.)
请注意以下行: $opts['http']['header' .= (点等于附加到数组元素。)

