php 如何使用 CURL 获取 HTTPS 的正文内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1215980/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:34:40  来源:igfitidea点击:

How to Get Body content of HTTPS using CURL

phpcurlhttps

提问by Stephen Baugh

The following code will retrieve the body content of a url retrieved using CURL in php but not https. Can anyone tell me how I edit the code as I need to get the data returned not just the header.

以下代码将检索在 php 中使用 CURL 检索的 url 的正文内容,但不检索 https。谁能告诉我如何编辑代码,因为我需要返回的数据不仅仅是标题。

From the test I did here is the result. You can see it has a content-length, I just don't know how to access it.

从我在这里做的测试是结果。你可以看到它有一个内容长度,我只是不知道如何访问它。

Thanks Stephen

谢谢斯蒂芬

Errors: 0

错误:0

string(1457) "HTTP/1.1 200 OK Date: Sat, 01 Aug 2009 06:32:11 GMT Server: Apache/1.3.41 (Darwin) PHP/5.2.4 mod_ssl/2.8.31 OpenSSL/0.9.7l Cache-Control: max-age=60 Expires: Sat, 01 Aug 2009 06:33:11 GMT Last-Modified: Thu, 23 Nov 2006 17:44:53 GMT ETag: "97d620-44b-4565de15" Accept-Ranges: bytes Content-Length: 1099 Connection: close Content-Type: text/html "

string(1457) "HTTP/1.1 200 OK 日期:2009 年 8 月 1 日星期六 06:32:11 GMT 服务器:Apache/1.3.41 (Darwin) PHP/5.2.4 mod_ssl/2.8.31 OpenSSL/0.9.7l Cache-控制:max-age=60 到期:2009 年 8 月 1 日星期六 06:33:11 GMT 上次修改时间:2006 年 11 月 23 日星期四 17:44:53 GMT ETag:“97d620-44b-4565de15” 接受范围:字节内容-长度:1099 连接:关闭内容类型:文本/html”

<?php

$curl_handle=curl_init();

$username = "";
$password = "";

$fullurl = "http://www.queensberry.com";
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_HEADER, 1);
   curl_setopt($ch, CURLOPT_VERBOSE, 1);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
   curl_setopt($ch, CURLOPT_FAILONERROR, 0);
   curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
   curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
   curl_setopt($ch, CURLOPT_URL, $fullurl);

   $returned = curl_exec($ch);

   curl_close ($ch);
   var_dump($returned);


?>

回答by Dinesh

Here is the solution: Try this, just keep rest of the coding same as above...

这是解决方案:试试这个,只需保持与上述相同的其余编码...

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
// curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_URL, $fullurl);

$returned = curl_exec($ch);

curl_close ($ch);
var_dump($returned);

Changing CURLOPT_HEADER to 0 makes it so that only the page content is returned.

将 CURLOPT_HEADER 更改为 0 使其仅返回页面内容。

回答by Josh

Shouldn't $fullurlbe "https://www.queensberry.com" ?

不应该$fullurl是“ https://www.queensberry.com”吗?

When I changed $fullurlas stated and ran the code, var_dumpdisplayed the "under construction" page.

当我$fullurl按照说明进行更改并运行代码时,var_dump显示“正在建设中”页面。

回答by KIC

if you still need the header, which means setting CURLOPT_HEADER to 0 is not an option, you can find the start of the body by looking for an empty line (two CRLF). See the spec: http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html

如果您仍然需要标题,这意味着将 CURLOPT_HEADER 设置为 0 不是一个选项,您可以通过查找空行(两个 CRLF)来找到正文的开头。请参阅规范:http: //www.w3.org/Protocols/rfc2616/rfc2616-sec6.html

so this should do the job:

所以这应该可以完成这项工作:

    $data = curl_exec($ch);
    $start = strpos($data, "\r\n\r\n") + 4;
    $body = substr($data, $start, strlen($data) - $start);

回答by fil

I look for the length of the header using the curl's getinfo. Then substring the response:

我使用 curl 的 getinfo 查找标题的长度。然后子字符串响应:

$info = curl_getinfo($ch);
$start = $info['header_size'];
$body = substr($result, $start, strlen($result) - $start);