Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 存在简单的卷曲url';行不通_Php_Http_Curl - Fatal编程技术网

Php 存在简单的卷曲url';行不通

Php 存在简单的卷曲url';行不通,php,http,curl,Php,Http,Curl,我有两个用PHP编写的代码 <?php error_reporting(1); function url_exists($url) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, TRUE); curl_setopt($ch, CURLOPT_NOBODY, TRUE); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE); curl_

我有两个用PHP编写的代码

<?php
error_reporting(1);

function url_exists($url) 
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_NOBODY, TRUE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $status = array();
    preg_match('/HTTP\/.* ([0-9]+) .*/', curl_exec($ch) , $status);
    return ($status[1] == 200);
}

echo "EX:".url_exists("http://www.google.com");

?>


以上这些都不行吗?为什么?昨天效果不错,为什么有些时候PHP无法解释

我在php.ini上有一个curl扩展。。。我试过3台不同的服务器

我做错了什么?多谢各位

echo "EX:".url_exists("http://www.google.com");
将始终回显
EX:
,因为
url\u存在
返回布尔值。尝试:

echo "EX:" . (url_exists("http://www.google.com") ? 'true' : 'false');
也许这样行吗

$ch = curl_init("http://www.example.com/favicon.ico");
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// $retcode > 400 -> not found, $retcode = 200, found.
curl_close($ch);

来源

什么不起作用?错误消息?什么都没有,只是不会返回1或0。。。相信我,如果google.com不存在的话,你的任何脚本都会返回1或0!请谷歌“js调试”。尝试
var_dump(curl_exec($ch))。另请参阅下面我对echo布尔值的编辑。第二个代码的var_dump(curl_exec($ch))返回bool(false)。。。天啊,这太令人沮丧了,昨天工作得很好,我从来没有碰过电脑,我已经安装了deepfreeze…第二个代码呢?它每次都返回“notfound”,不管我在那里放了什么链接,这就是我调试的意思<代码>echo$httpcode=>'302',谷歌httpcode 302=>重定向,谷歌curl follow重定向=>是的,你是对的,它返回了302,谢谢,我放了一些whare trim()来解决这个问题。。。哦php有时会很难,关于这一点,请遵循重定向,您只需写一行,witch我不需要,但谢谢
$ch = curl_init("http://www.example.com/favicon.ico");
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// $retcode > 400 -> not found, $retcode = 200, found.
curl_close($ch);