PHP curl显示URL的输出,如'HTTP / 1.1 200 OK'

I am trying to get the response/status code including the header for multiple URL's. My first try I was successful in getting the status code i.e. 200 or 301 or 302. But I want the output to be like HTTP/1.1 200 OK or HTTP/1.1 302 Found etc.

Below is my code in which I get just the response code i.e. 200 or 301.

<?php
$line = "https://www.pnc.com";
        $ch = curl_init($line);
        curl_setopt($ch, CURLOPT_URL, $line);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_HEADER,true);
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_setopt($ch, CURLOPT_TIMEOUT,10);
        $out = curl_exec($ch);
        $ret = true;
        if ($out !== false) {
                $statuscode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
                echo "Response Code:" .$statuscode. "";
        }
        curl_close($ch);
?>

Actual Ouput

Response Code:HTTP/1.1 200 OK
ETag: "846caf551746b12e876c0bad5a50830d:1475788950"
Last-Modified: Thu, 06 Oct 2016 21:22:30 GMT
Accept-Ranges: bytes
Content-Length: 17609
Content-Type: text/html
Expires: Fri, 21 Sep 2018 15:35:01 GMT
Cache-Control: max-age=0, no-cache, no-store
Pragma: no-cache
Date: Fri, 21 Sep 2018 15:35:01 GMT
Connection: keep-alive

Expected Output

HTTP/1.1 200 OK.

Can someone help me with this?

1个回答



这可以使用 strtok </ p>

示例:</ p>

  $ statuscode = strtok($ statuscode,“
“);
</ code> </ pre>

- 或 - </ p>

使用 explode 非常标准:</ p>

  $ statuscode = explode(”
“,$ statuscode )[0];
</ code> </ pre>

肯定还有其他方法</ p>

然而,可能最干净的方法可能是使用cURL 本身:</ p>

  $ response = curl_getinfo($ ch,CURLINFO_HTTP_CODE); 
</ code> </ pre>
</ div>

展开原文

原文

This can be done using strtok

Example:

$statuscode = strtok( $statuscode, "
" );

-or-

Using explode is pretty standard:

$statuscode = explode( "
", $statuscode )[0];

There are definitely other methods as well

However probably the cleanest way might be to use cURL itself:

$response = curl_getinfo( $ch, CURLINFO_HTTP_CODE );

duanchong3075
duanchong3075 我用过这个,但是我没有得到理想的结果。 此外,我试图硬编码值,所以我使用开关。 但仍然没有结果。
大约 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐