如何从 CURL 响应中删除 HTTP 标头?

发布于 2024-10-19 20:10:57 字数 1846 浏览 7 评论 0原文

我有一个 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

实际响应只是 6.8320 作为文本,没有任何 html。我想通过删除标题信息从上面的响应中检索它。

我已经稍微缩小了脚本:

$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;
}

知道我需要更改什么才能从响应中删除标头信息吗?

I have a php script that returns just plain text without any html. Now I want to make a cURL request to that script and I get the following response:

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.8320 as text without any html. I want to retrieve it from the response above by just removing the header information.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

把昨日还给我 2024-10-26 20:10:57

只需将 CURLOPT_HEADER 设置为 false 即可。

Just set CURLOPT_HEADER to false.

你是我的挚爱i 2024-10-26 20:10:57

确保设置了标头标志:

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, true );
        curl_setopt($ch, CURLOPT_TIMEOUT, Constants::HTTP_TIMEOUT);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, Constants::HTTP_TIMEOUT);
        $response = curl_exec($ch); 

在curl调用后执行此操作:

$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headerstring = substr($response, 0, $header_size);
$body = substr($response, $header_size);

编辑:
如果您想在 assoc 数组中包含标头,请添加如下内容:

    $headerArr = explode(PHP_EOL, $headerstring);
    foreach ($headerArr as $headerRow) {
        preg_match('/([a-zA-Z\-]+):\s(.+)$/',$headerRow, $matches);
        if (!isset($matches[0])) {
            continue;
        }
        $header[$matches[1]] = $matches[2];
    }

结果 print_r($header):

(
    [content-type] => application/json
    [content-length] => 2848
    [date] => Tue, 06 Oct 2020 10:29:33 GMT
    [last-modified] => Tue, 06 Oct 2020 10:17:17 GMT
)

不要忘记关闭连接 curl_close($ch);

Make sure you put set the header flag:

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, true );
        curl_setopt($ch, CURLOPT_TIMEOUT, Constants::HTTP_TIMEOUT);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, Constants::HTTP_TIMEOUT);
        $response = curl_exec($ch); 

Do this after your curl call:

$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headerstring = substr($response, 0, $header_size);
$body = substr($response, $header_size);

EDIT:
If you'd like to have header in assoc array, add something like this:

    $headerArr = explode(PHP_EOL, $headerstring);
    foreach ($headerArr as $headerRow) {
        preg_match('/([a-zA-Z\-]+):\s(.+)$/',$headerRow, $matches);
        if (!isset($matches[0])) {
            continue;
        }
        $header[$matches[1]] = $matches[2];
    }

Result print_r($header):

(
    [content-type] => application/json
    [content-length] => 2848
    [date] => Tue, 06 Oct 2020 10:29:33 GMT
    [last-modified] => Tue, 06 Oct 2020 10:17:17 GMT
)

Don't forget to close connection curl_close($ch);

夏尔 2024-10-26 20:10:57

将 CURLOPT_HEADER 的值更新为 0(表示 false)

curl_setopt($ch, CURLOPT_HEADER, 0);

Update the value of CURLOPT_HEADER to 0 for false

curl_setopt($ch, CURLOPT_HEADER, 0);
眼中杀气 2024-10-26 20:10:57

仅供以后其他人需要时使用。我遇到了同样的情况,但只需要删除标题文本,而不是内容。我在标头中得到的响应是(包括空格):

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

我想删除从 HTTP 开始直到使用空格保持活动状态:

$contents = preg_replace('/HTTP(.*)alive/s',"",$contents);

这对我来说是这样。

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:

$contents = preg_replace('/HTTP(.*)alive/s',"",$contents);

that did for me.

风吹雪碎 2024-10-26 20:10:57

如果您使用 nuSoap,则可以使用 $nsoap->responseData$nsoap->response 访问不带标头的数据(如果您想要完整的标题。

以防万一有人需要。

If you are using nuSoap, you can access data without headers with $nsoap->responseData or $nsoap->response, if you want the full headers.

Just in case someone needs that.

萤火眠眠 2024-10-26 20:10:57

例如,如果由于某种原因您必须 curl_setopt($ch, CURLOPT_HEADER, 1); 来获取 cookie,则以下内容对我有用。不确定它是否 100% 可靠,但值得一试

$foo = preg_replace('/HTTP(.*)html/s',"",$curlresult);

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

$foo = preg_replace('/HTTP(.*)html/s',"",$curlresult);
公布 2024-10-26 20:10:57
$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;
$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;
微凉徒眸意 2024-10-26 20:10:57

如果有人已经将curl响应保存到文件中(像我一样),因此不知道使用substr的标头有多大,请尝试:

$file = '/path/to/file/with/headers';
file_put_contents($file, preg_replace('~.*\r\n\r\n~s', '', file_get_contents($file)));

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:

$file = '/path/to/file/with/headers';
file_put_contents($file, preg_replace('~.*\r\n\r\n~s', '', file_get_contents($file)));
神也荒唐 2024-10-26 20:10:57

只是不要设置CURLOPT_HEADER

Just don't set CURLOPT_HEADER!

厌倦 2024-10-26 20:10:57

只是不要在curl请求中设置curl_header或将其设置为z或false
像这样
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HEADER, false);

Just do not set the curl_header in the curl request or set it to z or false
like this
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HEADER, false);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文