使用CURL重定向php脚本
我正在尝试制作一个重定向php脚本,我希望该脚本检查链接是否存在,然后将用户重定向到该链接,如果该链接不存在,那么它将获得下一个链接,依此类推,但由于某些原因无法工作,也许您可以在这方面给我一些帮助使用CURL重定向php脚本,php,redirect,curl,url-redirection,Php,Redirect,Curl,Url Redirection,我正在尝试制作一个重定向php脚本,我希望该脚本检查链接是否存在,然后将用户重定向到该链接,如果该链接不存在,那么它将获得下一个链接,依此类推,但由于某些原因无法工作,也许您可以在这方面给我一些帮助 <?php $URL = 'http://www.site1.com'; $URL = 'http://www.site2.com'; $URL = 'http://www.site3.com'; $handlerr = curl_init($URL); curl_setopt($handl
<?php
$URL = 'http://www.site1.com';
$URL = 'http://www.site2.com';
$URL = 'http://www.site3.com';
$handlerr = curl_init($URL);
curl_setopt($handlerr, CURLOPT_RETURNTRANSFER, TRUE);
$resp = curl_exec($handlerr);
$ht = curl_getinfo($handlerr, CURLINFO_HTTP_CODE);
if ($ht == '404')
{ echo "Sorry the website is down atm, please come back later!";}
else { header('Location: '. $URL);}
?>
这将为您提供url存在检查
要检查多个URL,您需要一个数组:
<?
$url_array = [];
$url_array[] = 'http://www.site1.com';
$url_array[] = 'http://www.site2.com';
$url_array[] = 'http://www.site3.com';
foreach ($url_array as $url) {
if url_exists($url){
// do what you need;
break;
}
}
?>
array( 'http://www.site1.com', 'http://www.site2.com', 'http://www.site3.com' );
PS-这是完全未经测试的,但理论上可以满足您的需要。
这将为您提供url存在检查
要检查多个URL,您需要一个数组:
<?
$url_array = [];
$url_array[] = 'http://www.site1.com';
$url_array[] = 'http://www.site2.com';
$url_array[] = 'http://www.site3.com';
foreach ($url_array as $url) {
if url_exists($url){
// do what you need;
break;
}
}
?>
array( 'http://www.site1.com', 'http://www.site2.com', 'http://www.site3.com' );
PS-这是完全未经测试的,但理论上可以满足您的需要。
这将为您提供url存在检查
要检查多个URL,您需要一个数组:
<?
$url_array = [];
$url_array[] = 'http://www.site1.com';
$url_array[] = 'http://www.site2.com';
$url_array[] = 'http://www.site3.com';
foreach ($url_array as $url) {
if url_exists($url){
// do what you need;
break;
}
}
?>
array( 'http://www.site1.com', 'http://www.site2.com', 'http://www.site3.com' );
PS-这是完全未经测试的,但理论上可以满足您的需要。
这将为您提供url存在检查
要检查多个URL,您需要一个数组:
<?
$url_array = [];
$url_array[] = 'http://www.site1.com';
$url_array[] = 'http://www.site2.com';
$url_array[] = 'http://www.site3.com';
foreach ($url_array as $url) {
if url_exists($url){
// do what you need;
break;
}
}
?>
array( 'http://www.site1.com', 'http://www.site2.com', 'http://www.site3.com' );
PS-这是完全未经测试的,但理论上应该满足您的需要。您正在覆盖
$URL
变量
$URL = 'http://www.site1.com';
$URL = 'http://www.site2.com';
$URL = 'http://www.site3.com';
将这些URL放在一个数组中,并对每个循环使用
。您正在覆盖$URL
变量
$URL = 'http://www.site1.com';
$URL = 'http://www.site2.com';
$URL = 'http://www.site3.com';
将这些URL放在一个数组中,并对每个循环使用。您正在覆盖$URL
变量
$URL = 'http://www.site1.com';
$URL = 'http://www.site2.com';
$URL = 'http://www.site3.com';
将这些URL放在一个数组中,并对每个循环使用。您正在覆盖$URL
变量
$URL = 'http://www.site1.com';
$URL = 'http://www.site2.com';
$URL = 'http://www.site3.com';
将这些URL放在一个数组中,并对每个循环使用。您的代码中有一些问题。对于1,您的$URL将覆盖自身,从而在其中仅产生1个URL。它需要是一个数组:
<?
$url_array = [];
$url_array[] = 'http://www.site1.com';
$url_array[] = 'http://www.site2.com';
$url_array[] = 'http://www.site3.com';
foreach ($url_array as $url) {
if url_exists($url){
// do what you need;
break;
}
}
?>
array( 'http://www.site1.com', 'http://www.site2.com', 'http://www.site3.com' );
您可以得到许多响应,而不仅仅是404,因此您应该告诉cURL遵循重定向。如果URL本身是一个重定向,则可能会得到一个301重定向到一个200。因此,我们希望遵循这一点
试试这个:
<?php
function curlGet($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ( $httpcode == 200 ) {
return true;
}
return false;
}
$urlArray = array( 'http://www.site1.com', 'http://www.site2.com', 'http://www.site3.com' );
foreach ( $urlArray as $url ) {
if ( $result = curlGet($url) ) {
header('Location: ' . $url);
exit;
}
}
// if we made it here, we looped through every url
// and none of them worked
echo "No valid URLs found...";
您的代码中有一些问题。对于1,您的$URL将覆盖自身,从而在其中仅产生1个URL。它需要是一个数组:
<?
$url_array = [];
$url_array[] = 'http://www.site1.com';
$url_array[] = 'http://www.site2.com';
$url_array[] = 'http://www.site3.com';
foreach ($url_array as $url) {
if url_exists($url){
// do what you need;
break;
}
}
?>
array( 'http://www.site1.com', 'http://www.site2.com', 'http://www.site3.com' );
您可以得到许多响应,而不仅仅是404,因此您应该告诉cURL遵循重定向。如果URL本身是一个重定向,则可能会得到一个301重定向到一个200。因此,我们希望遵循这一点
试试这个:
<?php
function curlGet($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ( $httpcode == 200 ) {
return true;
}
return false;
}
$urlArray = array( 'http://www.site1.com', 'http://www.site2.com', 'http://www.site3.com' );
foreach ( $urlArray as $url ) {
if ( $result = curlGet($url) ) {
header('Location: ' . $url);
exit;
}
}
// if we made it here, we looped through every url
// and none of them worked
echo "No valid URLs found...";
您的代码中有一些问题。对于1,您的$URL将覆盖自身,从而在其中仅产生1个URL。它需要是一个数组:
<?
$url_array = [];
$url_array[] = 'http://www.site1.com';
$url_array[] = 'http://www.site2.com';
$url_array[] = 'http://www.site3.com';
foreach ($url_array as $url) {
if url_exists($url){
// do what you need;
break;
}
}
?>
array( 'http://www.site1.com', 'http://www.site2.com', 'http://www.site3.com' );
您可以得到许多响应,而不仅仅是404,因此您应该告诉cURL遵循重定向。如果URL本身是一个重定向,则可能会得到一个301重定向到一个200。因此,我们希望遵循这一点
试试这个:
<?php
function curlGet($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ( $httpcode == 200 ) {
return true;
}
return false;
}
$urlArray = array( 'http://www.site1.com', 'http://www.site2.com', 'http://www.site3.com' );
foreach ( $urlArray as $url ) {
if ( $result = curlGet($url) ) {
header('Location: ' . $url);
exit;
}
}
// if we made it here, we looped through every url
// and none of them worked
echo "No valid URLs found...";
您的代码中有一些问题。对于1,您的$URL将覆盖自身,从而在其中仅产生1个URL。它需要是一个数组:
<?
$url_array = [];
$url_array[] = 'http://www.site1.com';
$url_array[] = 'http://www.site2.com';
$url_array[] = 'http://www.site3.com';
foreach ($url_array as $url) {
if url_exists($url){
// do what you need;
break;
}
}
?>
array( 'http://www.site1.com', 'http://www.site2.com', 'http://www.site3.com' );
您可以得到许多响应,而不仅仅是404,因此您应该告诉cURL遵循重定向。如果URL本身是一个重定向,则可能会得到一个301重定向到一个200。因此,我们希望遵循这一点
试试这个:
<?php
function curlGet($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ( $httpcode == 200 ) {
return true;
}
return false;
}
$urlArray = array( 'http://www.site1.com', 'http://www.site2.com', 'http://www.site3.com' );
foreach ( $urlArray as $url ) {
if ( $result = curlGet($url) ) {
header('Location: ' . $url);
exit;
}
}
// if we made it here, we looped through every url
// and none of them worked
echo "No valid URLs found...";
谢谢你,韦德,它工作起来很有魅力吗,你刚刚在第21行输入了一个错误,它是“{”而不是“}”,太完美了!打字错误修正。。别戴上我的眼镜:谢谢你,韦德,它工作起来像个符咒吗,你刚刚在第21行打错了,它是“{”而不是“}”,完美!打字错误修正。。别戴上我的眼镜:谢谢你,韦德,它工作起来像个符咒吗,你刚刚在第21行打错了,它是“{”而不是“}”,完美!打字错误修正。。别戴上我的眼镜:谢谢你,韦德,它工作起来像个符咒吗,你刚刚在第21行打错了,它是“{”而不是“}”,完美!打字错误修正。。不要戴上我的眼镜