2012-04-27 2 views
0

cURL없이 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

무엇 나타나는 오류입니까? –

+0

편집을 참조하십시오. 전체 오류를 표시하도록 코드를 약간 변경했습니다. –

답변

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; 
} 
당신이 원하는 무엇을

변경의 Content-Type .. JSON, XML 아무것도

관련 문제