2014-11-06 100 views
0

我一直在尋找解決方案,爲我的問題這麼長一段時間。 但我只是找不到任何解決方案,我不知道究竟要搜索什麼。PHP Post請求內容包含原始數據

我會在這裏解釋我的問題。 目前我有這個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); 
?> 

但我真的需要發送請求,內容如下幫助:

見截圖:http://i.imgur.com/VgedJes.png

我需要添加3頭,我不知道如何告訴字段/值是空的,你可以在屏幕截圖中看到。 我只是無法弄清楚如何做到這一點。

我真的很感激,如果有人能幫助我這個!

回答

0


要添加標題:

$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-請求標頭只能通過你的PHP應用程序來解釋

要使用的發送參數空值你可以做如下

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

同樣在您的PHP代碼中,您必須進行驗證:if(empty($_POST['notMandatoryParam'])) {/*SKIP ME*/}