php - 如何通过curl获取此网站的网页内容

时间:2017-04-30 13:26:24

标签: php curl php-curl

如何通过curl获取此网站的网页内容: divar。 IR

我想尝试从卷曲的网站上获取页面,但不能正常工作

我写了以下代码页404显示。

function get_web_page( $url )
{
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_USERAGENT      => "spider", // who am i
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects

        CURLOPT_SSL_VERIFYPEER => false     // Disabled SSL Cert checks


    );



    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
    return $header;
}



$link="https://divar.ir/";
$res =get_web_page($link);
echo $res['content'];

1 个答案:

答案 0 :(得分:0)

更改CURLOPT_USERAGENT

CURLOPT_USERAGENT => " Mozilla / 5.0(Windows NT 6.1; WOW64; rv:53.0)Gecko / 20100101 Firefox / 53.0",