[英]How to get the HTTP request as a string from PHP HTTP Stream
I'm using PHP Streams to make HTTP calls with fopen(). 我正在使用PHP流通过fopen()进行HTTP调用。 I would like print the HTTP request as a string (headers and content) before it is sent via fopen().
我想在通过fopen()发送HTTP请求之前将其打印为字符串(标头和内容)。
Is there a function that handles the stream context variable to do this? 是否有处理流上下文变量的函数来做到这一点? If so, would someone be kind enough to post a brief example of this?
如果是这样,有人会友善地举一个简短的例子吗?
Here is a function that expects a URL, fetches it and returns the content: 这是一个需要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.