php 如何从 CURL 响应中删除 HTTP 标头?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5142869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove HTTP headers from CURL response?
提问by Upvote
I have a php script that returns just plain text withoutany html. Now I want to make a cURL request to that script and I get the following response:
我有一个 php 脚本,它只返回纯文本,没有任何 html。现在我想向该脚本发出 cURL 请求,并得到以下响应:
HTTP/1.1 200 OK
Date: Mon, 28 Feb 2011 14:21:51 GMT
Server: Apache/2.2.14 (Ubuntu)
X-Powered-By: PHP/5.2.12-nmm2
Vary: Accept-Encoding
Content-Length: 6
Content-Type: text/html
6.8320
The actuall response is just 6.8320as text without any html. I want to retrieve it from the response above by just removing the header information.
实际响应只是6.8320作为没有任何 html 的文本。我想通过删除标头信息从上面的响应中检索它。
I already minified the script a bit:
我已经将脚本缩小了一点:
$url = $_GET['url'];
if ( !$url ) {
// Passed url not specified.
$contents = 'ERROR: url not specified';
$status = array( 'http_code' => 'ERROR' );
} else if ( !preg_match( $valid_url_regex, $url ) ) {
// Passed url doesn't match $valid_url_regex.
$contents = 'ERROR: invalid url';
$status = array( 'http_code' => 'ERROR' );
} else {
$ch = curl_init( $url );
if ( strtolower($_SERVER['REQUEST_METHOD']) == 'post' ) {
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $_POST );
}
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_HEADER, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_USERAGENT, $_GET['user_agent'] ? $_GET['user_agent'] : $_SERVER['HTTP_USER_AGENT'] );
list( $header, $contents ) = preg_split( '/([\r\n][\r\n])\1/', curl_exec( $ch ), 2 );
$status = curl_getinfo( $ch );
curl_close( $ch );
}
// Split header text into an array.
$header_text = preg_split( '/[\r\n]+/', $header );
if ( true ) {
if ( !$enable_native ) {
$contents = 'ERROR: invalid mode';
$status = array( 'http_code' => 'ERROR' );
}
// Propagate headers to response.
foreach ( $header_text as $header ) {
if ( preg_match( '/^(?:Content-Type|Content-Language|Set-Cookie):/i', $header ) ) {
header( $header );
}
}
print $contents;
}
Any idea what I need to change to remove the header information from the response?
知道我需要更改什么才能从响应中删除标头信息吗?
回答by Ewan Heming
Just set CURLOPT_HEADER
to false.
只需设置CURLOPT_HEADER
为false。
回答by Roeland Werring
Do this after your curl call:
在 curl 调用后执行此操作:
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
回答by Yaco Zaragoza
Update the value of CURLOPT_HEADER to 0 for false
将 CURLOPT_HEADER 的值更新为 0 为假
curl_setopt($ch, CURLOPT_HEADER, 0);
回答by Medalist
Just for a later use if anyone else needs. I was into same situation, but just need to remove header text, not content. The response i was getting in the header was (including white space):
如果其他人需要,仅供以后使用。我遇到了同样的情况,但只需要删除标题文本,而不是内容。我在标题中得到的响应是(包括空格):
HTTP/1.1 200 OK
Cache-Control: private, no-cache, no-store, must-revalidate
Content-Language: en
Content-Type: text/html
Date: Tue, 25 Feb 2014 20:59:29 GMT
Expires: Sat, 01 Jan 2000 00:00:00 GMT
Pragma: no-cache
Server: nginx
Vary: Cookie, Accept-Language, Accept-Encoding
transfer-encoding: chunked
Connection: keep-alive
I wanted to remove starting from HTTP till keep-alive with white space:
我想从 HTTP 开始删除,直到用空格保持活动状态:
$contents = preg_replace('/HTTP(.*)alive/s',"",$contents);
that did for me.
这对我有用。
回答by superjaz1
If for some reason you have to curl_setopt($ch, CURLOPT_HEADER, 1);
to get cookies for example, the following worked for me. Not sure if it's 100% reliable but worth a try
curl_setopt($ch, CURLOPT_HEADER, 1);
例如,如果由于某种原因您必须获取 cookie,以下内容对我有用。不确定它是否 100% 可靠,但值得一试
$foo = preg_replace('/HTTP(.*)html/s',"",$curlresult);
回答by NoSkill
$content = null;
$ch = curl_init();
$rs = curl_exec($ch);
if (CURLE_OK == curl_errno($ch)) {
$content = substr($rs, curl_getinfo($ch, CURLINFO_HEADER_SIZE));
}
curl_close($ch);
echo $content;
回答by Peon
If you are using nuSoap
, you can access data without headers with $nsoap->responseData
or $nsoap->response
, if you want the full headers.
如果您正在使用nuSoap
,则可以使用$nsoap->responseData
或访问没有标题的数据$nsoap->response
,如果您想要完整的标题。
Just in case someone needs that.
以防万一有人需要。
回答by Petr Nagy
If someone already saved the curl response to a file (like me) and therefore don't know how big the header was to use substr, try:
如果有人已经将 curl 响应保存到文件(像我一样),因此不知道使用 substr 的标头有多大,请尝试:
$file = '/path/to/file/with/headers';
file_put_contents($file, preg_replace('~.*\r\n\r\n~s', '', file_get_contents($file)));
回答by Shaibi Rana
Just do not set the curl_header
in the curl request or set it to z or false
like thiscurl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HEADER, false);
只是不要curl_header
在 curl 请求中设置或将其设置为z or false
这样curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HEADER, false);
回答by rik
Just don't set CURLOPT_HEADER
!
只是不要设置CURLOPT_HEADER
!