2011-10-19 27 views
0
function do_post_request($url, $data, $optional_headers = null) 
{ 
    $params = array('http' => array(
       'method' => 'POST', 
       'content' => $data 
      )); 
    if ($optional_headers !== null) { 
    $params['http']['header'] = $optional_headers; 
    } 
    $ctx = stream_context_create($params); 
    $fp = @fopen($url, 'rb', false, $ctx); 
if (!$fp) { 
    throw new Exception("Problem with $url, $php_errormsg"); 
    } 
    $response = @stream_get_contents($fp); 
    if ($response === false) { 
    throw new Exception("Problem reading data from $url, $php_errormsg"); 
    } 
    return $response; 
} 

不POST數組:處理HTTP郵政與陣列(沒有捲曲)

$postdata = array( 
    'send_email' => $_REQUEST['send_email'], 
    'send_text' => $_REQUEST['send_text']); 

我怎樣才能得到獨立的數組元素個別PHP變種? POST數據處理器的頁面

部分:

... 
$message = $_REQUEST['postdata']['send_text']; 
... 

什麼錯?

+0

你可以嘗試弄清楚到底是什麼問題,您有?你是否收到任何錯誤訊息?或者是與另一端的腳本相關的問題,即您要發送數據的頁面? – DaveRandom

+0

POST數據處理器不顯示任何內容,它什麼也收不到。 – Crone

+0

...所以如果你print_r($ _ REQUEST);'? – DaveRandom

回答

2

試試這個:

在客戶端:

function do_post_request ($url, $data, $headers = array()) { 
    // Turn $data into a string 
    $dataStr = http_build_query($data); 
    // Turn headers into a string 
    $headerStr = ''; 
    foreach ($headers as $key => $value) if (!in_array(strtolower($key),array('content-type','content-length'))) $headerStr .= "$key: $value\r\n"; 
    // Set standard headers 
    $headerStr .= 'Content-Length: '.strlen($data)."\r\nContent-Type: application/x-www-form-urlencoded" 
    // Create a context 
    $context = stream_context_create(array('http' => array('method' => 'POST', 'content' => $data, 'header' => $headerStr))); 
    // Do the request and return the result 
    return ($result = file_get_contents($url, FALSE, $context)) ? $result : FALSE; 
} 

$url = 'http://sub.domain.tld/file.ext'; 
$postData = array( 
    'send_email' => $_REQUEST['send_email'], 
    'send_text' => $_REQUEST['send_text'] 
); 
$extraHeaders = array(
    'User-Agent' => 'My HTTP Client/1.1' 
); 

var_dump(do_post_request($url, $postData, $extraHeaders)); 

在服務器端:

print_r($_POST); 
/* 
    Outputs something like: 
    Array (
     [send_email] => Some Value 
     [send_text] => Some Other Value 
    ) 
*/ 

$message = $_POST['send_text']; 
echo $message; 
// Outputs something like: Some Other Value 
+0

非常感謝。錯誤很簡單。 – Crone