2011-10-19 6 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 포스트 (NO 컬)

$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