2011-10-19 5 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ポスト(なしのcURL)

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

はどのようにして、個々のPHPのVARへの個々の配列要素を取得できますか? POSTデータプロセッサのページの

パート:

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

何が悪いのでしょうか?サーバー側で

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

:クライアント側で

+0

は、あなたがしようとすると、問題は、あなたが持っているということです正確に何を明確にすることはできますか?エラーメッセージが表示されますか?または、相手側のスクリプト、データを送信しているページに関連する問題ですか? – DaveRandom

+0

POSTデータプロセッサに何も表示されません。何も受信しません。 – Crone

+0

... 'print_r($ _ REQUEST);'? – DaveRandom

答えて

2

これを試してみてください

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