PHP HTTP Request (stream_context_create)


PHP HTTP Request (stream_context_create)

嗨,我正在努力让以下内容发挥作用。它发出HTTP请求是因为我可以在另一端看到它,但它没有传递两个值和参数。

有人能帮忙吗?

<?php
$url = "http://example.com/ws.php?uid=0000&pin=0000";
$fields = array(
    'target1' => $_GET['target1'],
    'target2' => $_GET['target2']);
$data = http_build_query($fields);
$context = stream_context_create(array(
'http' =>  array(
    'method'  => 'POST',
    'header'  => 'Content-type: application/x-www-form-urlencoded',
    'content' => $data,
)
));
$result = file_get_contents($url, false, $context);
echo $data."<br />";
echo($result); 
?>

如果是PHP 5.2,请参阅PHP错误https://bugs.php.net/bug.php?id=41051

说明:

http包装忽略streamcreate_context()中的"header"选项如果该值不是作为简单数组给出的;既不是字符串也不是映射(例如数组('X-My-Header'=>'test'))将导致要发送的标头。

根据我看到的示例,您需要在头值的末尾使用'r'n

'header'  => "Content-type: application/x-www-form-urlencoded'r'n" 

除此之外,您还需要提供

"Content-Length: " . strlen($data) . "'r'n",