Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从中获取数据php://input?_Php - Fatal编程技术网

如何从中获取数据php://input?

如何从中获取数据php://input?,php,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($_SER

我有两个脚本:

首先发送:

$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。

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

可能很明显,但是$incoming是空的吗?是的,我错了$传入字段也为空。请使用
CURLOPT_POSTFIELDS
而不是将字段附加到URI(使其获取参数)。