doudao7511 2011-10-25 14:54
浏览 19

需要将fsocketopen更改为CURL才能导航重定向

I have a php script that I recently added an array to. The php script checks URL's from the array for a set of text also in the array. Everything works great except the script will not follow redirects or check sub-pages. I have been told that this is a limitation of fsocketopen and that I need to use CURL. If this is the case then I require some assistance converting this from using fsocketopen to CURL. Hopefully there is some way to get fsocketopen to follow redirects or at least access sub-pages.

function check($host, $find){
    $fp = fsockopen($host, 80, $errno, $errstr, 10);
    if (!$fp){
        echo "$errstr ($errno)
";
    } else {
        $header = "GET / HTTP/1.1
";
        $header .= "Host: $host
";
        $header .= "Connection: close

";
        fputs($fp, $header);
        while (!feof($fp)) {
            $str.= fgets($fp, 1024);
        }
        fclose($fp);
        return (strpos($str, $find) !== false);
    }
}

function alert($host){
    $headers = 'From: Set your from address here';
    mail('my-email@my-domain.com', 'Website Monitoring', $host.' is down' $headers);
}

$hostMap = array(
    'www.my-domain.com' => 'content on site',
    'www.my-domain2.com' => 'content on second site',
);

//if (!check($host, $find)) alert($host);
foreach ($hostMap as $host => $find){
    if( !check( $host, $find ) ){
        alert($host);
    }
}
unset($host);
unset($find);

Apparently I wasn't clear in my question. I am looking for confirmation that fsocketopen cannot follow redirects or that it cannot go to a sub-page (url.com/subpage). If this is the case, is CURL my best option and are there any examples I can look at?

  • 写回答

1条回答 默认 最新

  • dongqing483174 2011-10-25 17:08
    关注

    When you get the data returned from feof(), you can parse out the redirect information from the headers and create connect to that, but that is kind of annoying and cURL does it on its own.

    You should just be able to use

    $ch = curl_init($host);
    curl_setopt_array($ch, array(
       CURLOPT_RETUNTRANSFER => true
       , CURLOPT_FOLLOWLOCATION => true
       , CURLOPT_HEADER => true
    ));
    $str = curl_exec($ch);
    

    cURL follows redirects by default using the Location: header.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?