PHP Post Request内容包含原始数据


PHP Post Request content contains Raw Data

我已经为我的问题寻找了很长时间的解决方案。但我就是找不到任何解决方案,也不知道该寻找什么。

我会在这里解释我的问题。目前,我有这个PHP脚本,它运行得很好,可以成功地发送请求并接收响应。

<?php
$url = 'http://example.nl/index.php';
$data = array('url' => 'http://www.myip.nl/', 'verkort' => 'Verkort URL');
// use key 'http' even if you send the request to https://...
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded'r'n",
        'method'  => 'POST',
        'content' => http_build_query($data),
    ),
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
?>

但我真的需要帮助发送以下内容的请求:

请参阅屏幕截图:https://i.stack.imgur.com/PH376.png

我需要添加3个标题,我不知道如何判断字段/值为空,正如您在屏幕截图中看到的那样。我就是不知道该怎么做。

如果有人能帮我解决这个问题,我将不胜感激!


添加标题:

$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded'r'nX-Requested-With: XmlHttpRequest",
        'method'  => 'POST',
        'content' => http_build_query($data),
    ),
);

请记住,X-Requested-Header只能由您的PHP应用程序解释

要发送一个空值的参数,可以按照

$data = array('url' => 'http://www.myip.nl/', 'verkort' => 'Verkort URL', 'notMandatoryParam' => '');

同样在PHP代码中,您必须进行验证:if(empty($_POST['notMandatoryParam'])) {/*SKIP ME*/}