【问题标题】:cURL is_file function for URLs用于 URL 的 cURL is_file 函数
【发布时间】:2013-09-17 21:06:50
【问题描述】:

我正在尝试创建一个适用于 URL 的简单 is_file 类型函数:

function isFileUrl($url){
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // don't want it rendered to browser
  curl_exec($ch);

  if(curl_errno($ch)){
    $isFile = false;
  }
  else {
    $isFile = true;
  }
  curl_close($ch);
  return $isFile;
}

if(isFileUrl('https://www.google.com/images/srpr/logo4w.png')==true){
  echo 'TRUE';
} else {
  echo 'FALSE';
}

curl_setopt 删除一起使用,但将内容(图像 url)呈现给浏览器,这是我不想要的。我错过了什么?我有checked this similar thread,但无法使其在我的上下文中工作。谢谢。

【问题讨论】:

  • don't want it rendered to browser 是什么意思?
  • 如果没有curl_setopt 行,如果我在if 语句if(isFileUrl('http://www.image.url.jpg')===true){} 中调用该函数,它会将url(在线托管的图像)呈现到页面,如果为真,The specified request cannot be executed from current Application Pool 如果为假。我不想要那个,只有输出的布尔值。
  • 那么,它 url_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 行有效吗?你说没有它“工作”,但你从不说那条线会发生什么。
  • @Rocket 总是返回 false ,其中包含该行。更新了我如何称呼它的问题。
  • 我刚刚在我的服务器上测试了这段代码,它运行良好。

标签: php curl


【解决方案1】:

为什么不使用get_headers() 函数?它就是为这些东西而设计的。

简单示例如下所示:

function isFile($url) {
    $headers = get_headers($url);
    if($headers && strpos($headers[0], '200') !== false) { //response code 200 means that url is fine
        return true;
    } else {
        return false;
    }
}

您还可以根据需要检查内容类型或任何其他标题。

【讨论】:

  • 那很好,我不知道我们有这个功能......我会添加一个 content_type 验证并删除 return false.. return false 是隐式的。
  • 不客气,约翰。 @enapupe,我之前也不知道。刚找到。 ;)
  • @dragoste 接受了 enapupe,只是因为它根据原始问题使用了 cURL 方法。您的回答也值得 +1。
【解决方案2】:

我认为您想要的是一个 HTTP 标头检查,以查看它是否是有效的 (200) url 并且它与图像相关(内容类型 === image/jpg、image/png 等)。 下面的代码可能是一个开始:

function getHTTPStatus($url) {
    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_VERBOSE => false
    ));
    curl_exec($ch);
    $http_status = curl_getinfo($ch);
    return $http_status;
}

$img = getHTTPStatus($url);
$images = array('image/jpeg', 'image/png', 'etc');
if($img['http_code'] === 200 && in_array($img['content_type'], $images)) {
   //it is an image, do stuff
 }

【讨论】:

  • CURLOPT_SSL_VERIFYPEER 可能是解决他遇到的问题的方法:-)
  • @RocketHazmat 我认为 SSL 问题与他的主要目标没有任何关系。
  • 在评论中,他说curl_error显示SSL certificate problem, verify that the CA cert is OK.
  • 这也返回 false/false。
  • 不,@John,它返回请求 URL 的 http 标头,这不是真/假的问题。
【解决方案3】:

(自我回答/总结)

Dragoste 和 Enapupe 的回答都有效。根据 Enapupe 的回答,我将它包装成一个包罗万象的函数,并添加了一个可选的文件类型参数。希望它可以帮助某人...

function isFileUrl($url, $type=''){
    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_VERBOSE => false
    ));
    curl_exec($ch);
    $gh = curl_getinfo($ch);

    if($type!==''){
      // add types here (see http://reference.sitepoint.com/html/mime-types-full for list)
      if($type=='img'){
        $typeArr = array('image/jpeg', 'image/png');
      }
      elseif($type=='pdf'){
        $typeArr = array('application/pdf');
      }
      elseif($type=='html'){
        $typeArr = array('text/html');
      }

      if($gh['http_code'] === 200 && in_array($gh['content_type'], $typeArr)){
        $trueFalse = true;
      }
      else {
        $trueFalse = false;
      }

    }
    else {
      if($gh['http_code'] === 200){
        $trueFalse = true;
      }
      else {
        $trueFalse = false;
      }
    }
    return $trueFalse;
}

//test - param 1 is URL, param 2 (optional) can be img|pdf|html
if(isFileUrl('https://www.google.com/images/srpr/logo4w.png', 'img')==true){
  // do stuff
  echo 'TRUE';
}
else {
  echo 'FALSE';
}

【讨论】:

    猜你喜欢
    • 2010-09-20
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多