2012-04-27 41 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什么