如何从php://输入获取数据?

时间:2017-05-03 17:41:21

标签: php

我有两个脚本:

首先发送:

$url = "http://localhost/curl2.php";
$data = array('email' => 'test@example.com');
$addr = $url . '?' . http_build_query($data);
$ch = curl_init($addr);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_exec($ch);

第二个接收:

if($_SERVER['REQUEST_METHOD'] == "PUT") {

    $data = array();
    $incoming = file_get_contents("php://input");
    parse_str($incoming, $data);
    echo "Address: " . filter_var($data["email"], FILTER_VALIDATE_EMAIL);
}

但是变量$ incoming是空的。我该怎么做?也许它与PUT有关,但我必须使用PUT。

1 个答案:

答案 0 :(得分:1)

PUT不是GET。使用不带查询字符串的URL,并将数据添加到CURLOPT_POSTFIELDS

$url  = "http://localhost/curl2.php";
$data = array('email' => 'test@example.com');
$ch   = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_exec($ch);