【问题标题】:How to check whether a pdf file exists from url?如何从url检查pdf文件是否存在?
【发布时间】:2015-11-10 08:38:54
【问题描述】:

我正在尝试检查 arXiv 网址是否为格式

http://arxiv.org/pdf/[some4位数字].[一些4位数字].pdf

是一个 pdf 文件或 html 页面。我的功能目前看起来像

function is_url_exist($url){
    $ch = curl_init($url);    
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if($code == 200){
       $status = "true";
    }else{
      $status = "false";
    }
    curl_close($ch);
   return $status;
}

问题是该函数对http://arxiv.org/pdf/1207.0102.pdf(pdf 文件)和http://arxiv.org/pdf/1217.2314.pdf(html 页面)都返回 false。有没有办法解决这个问题?

【问题讨论】:

  • 那么你可以在if 声明之前开始转储$code,这样你就可以看到你实际上从中得到了什么。你做到了吗?
  • 这两种情况我都得到了 403,这个数字意味着什么吗?
  • There you go,然后。
  • @AedixRhinedale,有没有办法解决这个问题?
  • 如果您收到 403 Forbidden,则表示您无权访问该内容。如果您无法从该位置访问数据,您将一无所获。获得授权,然后返回您的代码。

标签: php html file pdf file-get-contents


【解决方案1】:

为什么不只检查扩展名?

<?PHP 
if(strtolower(substr($url, -3, 3)) == "pdf"){
    // Do something with pdf
} else {
    // Not pdf
} 
?>

【讨论】:

  • 他(奇怪地)举了一个例子,其中 URL 显示 .pdf 扩展名是 html 文档。
猜你喜欢
  • 2011-12-02
  • 1970-01-01
  • 2012-08-09
  • 1970-01-01
  • 2020-07-05
  • 1970-01-01
  • 2020-06-25
  • 1970-01-01
  • 2013-09-16
相关资源
最近更新 更多