簡體   English   中英

如何從PHP HTTP Stream獲取HTTP請求作為字符串

[英]How to get the HTTP request as a string from PHP HTTP Stream

我正在使用PHP流通過fopen()進行HTTP調用。 我想在通過fopen()發送HTTP請求之前將其打印為字符串(標頭和內容)。

是否有處理流上下文變量的函數來做到這一點? 如果是這樣,有人會友善地舉一個簡短的例子嗎?

這是一個需要URL的函數,將其獲取並返回內容:

function do_post_request ($url, $content, $cookie = null) {
  $headers = array("Content-type: application/x-www-form-urlencoded",
                   $cookie);

  $all_headers = implode("\r\n", $headers)."\r\n";
  $params = array('http' => array("method" => "POST",
                                  "content" => $content,
                                  "header" => $all_headers,
                                 ),
                 );

  $ctx = stream_context_create($params);
  $fp = fopen($url, 'rb', false, $ctx);
  if (empty($fp)) {
    throw new Exception("$url: Failed");
  }
  $resp = stream_get_contents($fp);

  if (empty($resp)) {
    throw new Exception("error: bad fetch");
  }
  return $resp
}

我將使用PEAR的HTTP_Request2 ,它支持攔截和記錄發送的標頭。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM