doupa8922 2018-09-21 15:37 采纳率: 100%
浏览 1208

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条回答 默认 最新

  • duangai1941 2018-09-21 15:46
    关注

    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 );
    
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站