2012-04-27 74 views
0

在沒有捲曲的情況下執行PHP調用(在此處找到:http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/),但響應大概需要10-15秒才能返回。它目前只是出現了一個錯誤。任何想法如何得到這個工作?我試過set_time_limit無濟於事。PHP無捲髮等待回覆

代碼:

function DoPostRequest($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); 
    try { 
     $fp = fopen($url, 'rb', false, $ctx); 
     $response = stream_get_contents($fp); 
    } catch (Exception $e) { 
     echo 'Exception: '.$e->getMessage(); 
    } 
    return $response; 
} 

和錯誤:

Notice: fopen(): Content-type not specified assuming application/x-www-form-urlencoded in <php url> on line 81 Warning: fopen(http://localhost:59396/Update.ashx): failed to open stream: HTTP request failed! HTTP/1.1 100 Continue in <php url> on line 81 Warning: stream_get_contents() expects parameter 1 to be resource, boolean given in <php url> on line 82 bool(false) 
+1

什麼是出現的錯誤? – 2012-04-27 15:23:15

+0

請參閱編輯。我稍微更改了代碼以顯示完整的錯誤。 – 2012-04-27 15:33:06

回答

6

嘗試

function DoPostRequest($url, $data, $optional_headers = null) { 
    $params = array (
      'http' => array (
        'method' => 'POST', 
        'content' => $data, 
        'header' => "Content-type: application/x-www-form-urlencoded\r\n" . 
        "Content-Length: " . strlen ($data) . "\r\n" 
      ) 
    ); 
    if ($optional_headers != null) { 
     $params ['http'] ['header'] = $optional_headers; 
    } 
    $ctx = stream_context_create ($params); 
    try { 
     $fp = fopen ($url, 'rb', false, $ctx); 
     $response = stream_get_contents ($fp); 
    } catch (Exception $e) { 
     echo 'Exception: ' . $e->getMessage(); 
    } 
    return $response; 
} 

更改內容類型你想要什麼.. JSON,XML什麼