1

我想获取我拥有它们的所有页面内容 url

我编写了以下 php 代码来获取https://fonts.googleapis.com/css?family=Open+Sans的内容

 function curl_file_get_content($url){
    $agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
    $ch=curl_init();
    curl_setopt_array($ch, [
            CURLOPT_URL=>$url,
            CURLOPT_USERAGENT=>$agent,
            CURLOPT_RETURNTRANSFER=>1,
            CURLOPT_TIMEOUT=>5,
            CURLOPT_VERBOSE=>0,
            CURLOPT_SSLVERSION=>3,
            CURLOPT_SSL_VERIFYPEER=>0,
            CURLOPT_SSL_VERIFYHOST=>0,
        ]);
    $page=curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if($httpcode>=200 && $httpcode<300) return $page;
    else return false;
}
if($content=curl_file_get_content("https://fonts.googleapis.com/css?family=Open+Sans")){
    echo $content;
}else{
    echo "the Website is DOWN" ;
}

输出是以下代码

@font-face {
  font-family: 'Open Sans';
  font-style: normal;
  font-weight: 400;
  src: url(https://fonts.gstatic.com/s/opensans/v13/cJZKeOuBrn4kERxqtaUH3fY6323mHUZFJMgTvxaG2iE.eot);
}

如果您查看 url https://fonts.googleapis.com/css?family=Open+Sans您会注意到此页面的内容,并且我的代码输出是不同的,并且我的代码输出是该页面的一部分

但是此代码可以正常工作以获取 localhost 文件的内容

有什么问题?我可以得到这个页面的全部内容吗?

4

1 回答 1

3

这是您使用的用户代理,当我将其设置为它时,Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36它似乎返回了应有的完整内容。

function fetch($url) {
    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL => $url,
        CURLOPT_USERAGENT => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_TIMEOUT => 5,
        CURLOPT_VERBOSE => 0,
        CURLOPT_SSLVERSION => 3,
        CURLOPT_SSL_VERIFYPEER => 0,
        CURLOPT_SSL_VERIFYHOST => 0,
        ]);
    $page = curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return array($code, $page);
}

list ($code, response) = fetch('https://fonts.googleapis.com/css?family=Open%20Sans');

if ($code == 200) {
    echo $response;
} else {
    echo 'Unable to fetch content: ' . $code;
    echo 'Response: ' . $response;
}
于 2016-07-05T13:12:26.733 回答