2012-02-09 86 views
1

The Post看起來像這樣。

$data = array('icd9' => $icd9, 'cpt' => $cpt, 'session_id'=> $session_id); 
    $url = $api_location."rarity/".$session_id; 
    $params = array('http' => array('method' => 'POST', 'content' => $data)); 
    $ctx = stream_context_create($params); 
    $fp = @fopen($url, 'rb', false, $ctx); 
    if (!$fp){ 
     return "Error"; 
    } 
    $response = @stream_get_contents($fp); 

    $results = $response;//json_decode($response); 

    return $results; 

的api_location服務器上的座看起來是這樣的:

if ($_POST['session_id']){ 
    $fh = fopen('/interaction/rarity_req/alive.txt','w'); 
    fwrite($fh,'\r\n'.date("Y-m-d - H:i:s")); 
    fclose($fh); 
    $session_id = $_POST['session_id']; 
    $cpt = $_POST['cpt']; 
    $icd9 = $_POST['icd9']; 
}else{ 
    $fh = fopen('/interaction/rarity_req/died.txt','w'); 
    fwrite($fh, '\r\n'.date("Y-m-d - H:i:s")); 
    fclose($fh); 
} 

什麼想法?我知道我可以安裝cURL來做到這一點,但是儘可能地避免它,因爲這很簡單。在立足它關閉這個博客帖子的:http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/

回答

3

把你的數據轉換爲URL編碼字符串:

$params = array('http' => array('method' => 'POST', 'content' => http_build_query($data))); 
+0

真棒就是這樣。我會在2分鐘內接受這個答案。 – DiscontentDisciple 2012-02-09 21:14:45

0

我沒有看到你的POST數據與http_build_query也不手動編碼。如果不這樣做,您需要讀取目標端的原始請求內容並手動解析它:-)

0

看起來您使用的數據變量格式不正確。檢查this comment在PHP手冊頁