【问题标题】:Redirect php script using CURL使用 CURL 重定向 php 脚本
【发布时间】:2015-05-26 17:20:03
【问题描述】:

我正在尝试制作一个重定向 php 脚本,我希望该脚本检查链接是否存在,然后将用户重定向到该链接,如果它不存在,那么它将获得下一个链接,依此类推,但是由于某种原因无法正常工作,也许您可​​以为此提供一些帮助。

<?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);}
?>

【问题讨论】:

    标签: php redirect curl url-redirection


    【解决方案1】:

    您正在覆盖您的 $URL 变量..

    $URL = 'http://www.site1.com';
    $URL = 'http://www.site2.com';
    $URL = 'http://www.site3.com';
    

    将这些 url 放入一个数组中,并使用 for each 循环遍历它。

    【讨论】:

      【解决方案2】:

      您的代码中存在一些问题。对于 1,您的 $URL 将覆盖自身,导致其中只有 1 个 url。它需要是一个数组:

      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 行打错字了,它是“{”而不是“}”,完美!
      • 错字已修复..不要戴眼镜:D
      【解决方案3】:

      http://php.net/manual/en/function.file-exists.php#74469

      <?php
      function url_exists($url) {
          if (!$fp = curl_init($url)) return false;
          return true;
      }
      ?>
      

      这将为您提供 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;
          }
      }
      
      ?>
      

      PS - 这完全未经测试,但理论上应该可以满足您的需求。

      【讨论】:

        猜你喜欢
        • 2017-03-02
        • 1970-01-01
        • 2010-10-25
        • 1970-01-01
        • 2013-02-02
        • 2015-06-08
        • 2011-04-05
        • 2011-08-31
        • 2021-05-18
        相关资源
        最近更新 更多