[英]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.