2012-04-27 12 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の何