检查网址是否可访问,始终返回false

时间:2018-09-25 11:52:46

标签: php

我的问题是,它为每个输入的URL返回一个错误:

下面的代码接受并检查输入的URL,如果我输入“ http://eample.com”,它将返回成功,但由于某种原因,它将所有URL返回为不可访问。

<?php
Global $excludeLocal;
$excludeLocal = true; // Whether to exclude checking links on the same host as the plugin resides

// Hook our custom function into the 'shunt_add_new_link' filter
 yourls_add_filter( 'shunt_add_new_link', 'churl_reachability' );

// Add a new link in the DB, either with custom keyword, or find one
function churl_reachability( $churl_reachable, $url, $keyword = '' )     {
   global $ydb, $excludeLocal;

// Check if the long URL is a different type of link
$different_urls = array (
    array ( 'mailto://', 9 ),
    array ( 'ftp://', 6 ),
    array ( 'javascript://', 13),
    array ( 'file://', 7 ),
    array ( 'telnet://', 9),
    array ( 'ssh://', 6),
    array ( 'sip://', 6),
    );

foreach ($different_urls as $url_type){
    if (substr( $url, 0, $url_type[1] ) == $url_type[0]){
        $churl_reachable = true; // No need to check reachability if URL type is different
        break;
    } elseif ($excludeLocal) {
        if (substr($url, 0, strlen('http://'.$_SERVER['SERVER_NAME'])) == 'http://'.$_SERVER['SERVER_NAME']) {
            $churl_reachable = true;
            break;
        }
    }
}

// Check if the long URL is a mailto
if ($churl_reachable == false){
    $churl_reachable = churl_url_exists( $url );  // To do: figure out how to use yourls_get_remote_content( $url ) instead.
}

// Return error if the entered URL is unreachable
if ( $churl_reachable == false ){
    $return['status']   = 'fail';
    $return['code']     = 'error:url';
    $return['message']  = 'The entered URL is unreachable.  Check the URL or try again later.';
    $return['statusCode'] = 200; // regardless of result, this is still a valid request
    return yourls_apply_filter( 'add_new_link_fail_unreachable', $return, $url, $keyword, $title );
} else {
    return false;
   }
 }

function churl_url_exists( $churl ){
    $handle = @fopen($churl, "r");
    if ($handle === false)
        return false;
        fclose($handle);
        return true;
   } 

我可能做错了什么吗?

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

在您的php.ini中

检查allow_url_fopen指令

尝试运行不带$handle = fopen($churl, "r");符号的命令@并查看错误