我正在使用CURL来获取站点的状态,如果它是向上/向下或重定向到另一个站点.我希望尽可能简化它,但效果不佳.
<?php
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_TIMEOUT,10);
$output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $httpcode;
?>
Run Code Online (Sandbox Code Playgroud)
我把它包裹在一个函数中.它工作正常,但性能不是最好的,因为它下载整个页面,如果我删除$output = curl_exec($ch);它返回0所有的时间.
有谁知道如何使性能更好?
我能找到的最好的,if fclose fopen类型的东西,使页面加载非常缓慢.
基本上我要做的是以下内容:我有一个网站列表,我想在他们旁边显示他们的favicon.但是,如果一个站点没有,我想用另一个图像替换它而不是显示损坏的图像.
我想要做的是找出重定向后的最后/最后一个URL.
我宁愿不使用cURL.我想坚持使用纯PHP(流包装器).
现在我有一个URL(让我们说http://domain.test),我使用get_headers()来获取该页面的特定标题.get_headers还将返回多个Location:标头(请参阅下面的编辑).有没有办法使用这些标头来构建最终的URL?或者是否有自动执行此操作的PHP函数?
编辑: get_headers()遵循重定向并返回每个响应/重定向的所有Location:标头,所以我有所有标头.
当用户点击链接时,HTML或JavaScript中是否有任何选项,如果该链接可用则完成,如果没有,则进入另一个链接.
这是我的代码的一部分:
<div id="sidebar-collapse" class="col-sm-3 col-lg-2 sidebar">
<ul>
<li><a href="/returns"><span class="glyphicon glyphicon-log-in"></span> Returns</a></li>
<li class="active"><a href="/pay_reco" target="_blank"><span class="glyphicon glyphicon-log-in"></span> Payment Recouncillation </a></li>
<li ><a href="/pay_get" target="_blank"><span class="glyphicon glyphicon-log-in"></span> Payment Get </a></li>
<li><a href="/import"><span class="glyphicon glyphicon-log-in"></span> Import </a></li>
<li>
<!-- Here I want to add Two link in a href tag if first link does not
available (web page not available) then it go to another link
<!-- <object data="http:/127.0.0.1:9004" type="href" > -->
<a target="_blank" href="http://127.0.0.1:9001">
<span class="glyphicon glyphicon-log-in"></span>
ComPare
</a> …Run Code Online (Sandbox Code Playgroud) 运行以下代码
var_dump(get_headers("http://www.domainnnnnnnnnnnnnnnnnnnnnnnnnnnn.com/CraxyFile.jpg"));
Run Code Online (Sandbox Code Playgroud)
返回HTTP 200而不是404对于任何不存在的域或URL
Array
(
[0] => HTTP/1.1 200 OK
[1] => Server: nginx/1.1.15
[2] => Date: Mon, 08 Oct 2012 12:29:13 GMT
[3] => Content-Type: text/html; charset=utf-8
[4] => Connection: close
[5] => Set-Cookie: PHPSESSID=3iucojet7bt2peub72rgo0iu21; path=/; HttpOnly
[6] => Expires: Thu, 19 Nov 1981 08:52:00 GMT
[7] => Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
[8] => Pragma: no-cache
[9] => Set-Cookie: bypassStaticCache=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; httponly
[10] => Set-Cookie: bypassStaticCache=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; httponly …Run Code Online (Sandbox Code Playgroud) 如何修复php警告:file_get_contents?
Warning: file_get_contents(http://192.168.1.254:9999/api/p/internal/ucenter/login?loginName=test102&password=111111) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /Applications/XAMPP/xamppfiles/htdocs/PHP_test/index.php on line 49
Run Code Online (Sandbox Code Playgroud)
这是与$ files相关的代码:
<?php
$loginNames="test102";
$passwords="111111";
$apiUrl = "http://192.168.1.254:9999/api/p/internal/ucenter/login?loginName=".$loginNames."&password=".$passwords;
$callback = file_get_contents($apiUrl);
print_r($callback);
//echo $callback;
?>
Run Code Online (Sandbox Code Playgroud) 我正在尝试检查某个域是否存在.我的想法是用file_get_contents()读取内容,并检查它是成功还是失败.
$line = file_get_contents('http://www.domain.com');
if ($line==false)
echo 'Domain is dead';
else
echo 'Domain is live';
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,当它失败时,它会在网页上输出警告.通过PHP配置关闭所有警告不是一个选项,因为我需要在其他一些部分.有没有办法让这个单一语句不输出警告?
或者有更好的方法来检查域名是否有效?我试过checkdnsrr()但它很慢.
我正在尝试获取URL标头的输出,并且它适用于子域示例
subdomain.example.com
Run Code Online (Sandbox Code Playgroud)
我将获得正常消息HTTP 200 ok等,但如果子域中有-其他内容,则标题中不会显示任何内容.
-test.tumblr.com 要么 test-.tumblr.com
有没有更好的方法呢?我的示例代码如下.
$url = "http://-test.tumblr.com";
$url_headers = @get_headers($url);
$urlheader = $url_headers[0];
echo $urlheader;
Run Code Online (Sandbox Code Playgroud)
如果无法检查标题,我将如何查看页面是否存在.我尝试过使用curl,但它也是如此.
谢谢
我通过检查HTTP 200状态代码检查站点站点是否已启动并运行:
<?php
$url = 'www.proxyserver-demo.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if (200==$retcode)
{
echo "All's well";
}
else
{
echo "not so much";
}
?>
Run Code Online (Sandbox Code Playgroud)
然而,我尝试的每个站点,即使是不存在的站点都会返回200 OK状态,这在不存在的情况下是不可能的.(我的代码总是输出"All's well")
我的操作系统是Centos linux,这可能是什么问题?
php ×9
curl ×2
http ×2
http-headers ×2
file ×1
html ×1
javascript ×1
jquery ×1
laravel ×1
networking ×1
performance ×1
testing ×1
url ×1
validation ×1
web ×1