在depositfiles dfiles.ru上有php cURL的问题

时间:2014-05-05 12:45:24

标签: php cookies curl

有一个问题:当我尝试使用php cURL在Dfiles dot ru(depositfiles)上进行身份验证时 - 我使用他们的API没有问题,例如:

    <?php 
$login = 'uName';
$password = 'uPass';
$url = 'http://dfiles.ru/api/user/login';
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($curl,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 6.0; ru; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3');
curl_setopt($curl,CURLOPT_POSTFIELDS,"login=".$login."&password=".$password);
curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt');

$content = curl_exec($curl);
curl_close($curl); 
echo $content; ?>

在使用auth完成后,我正在使用cookie文件进行下一步操作,我想以cURL的授权用户身份打开起始页:

    <?php
$header = array ("Accept-Encoding: gzip, deflate", "Content-Type: text/html");
$ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_REFERER, $url);
   curl_setopt($ch, CURLOPT_HEADER, 1);
   curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
   curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
   curl_setopt($ch, CURLOPT_POST, false);
   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
   curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.0; En expl; rv:1.8.0.2) Gecko/20070306 Firefox/1.0.0.5");

   $result = curl_exec($ch);

   curl_close($ch);

   return $result;
?>

但是我看到了 - 白屏:)也许我错过了什么,但我还包括标题......

3 个答案:

答案 0 :(得分:0)

您错过了CURLOPT_COOKIESESSION。试试以下内容:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile");

答案 1 :(得分:0)

您当前的配置不允许CURLOPT_FOLLOWLOCATION

请改为尝试:

<?php 
$login = 'uName';
$password = 'uPass';
$url = 'http://dfiles.ru/api/user/login';
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($curl,CURLOPT_POSTFIELDS,"login=".$login."&password=".$password);
curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt');

$content = curl_exec_follow($curl);
curl_close($curl); 
echo $content; 


    function curl_exec_follow($ch, &$maxredirect = null) {

      // we emulate a browser here since some websites detect
      // us as a bot and don't let us do our job
      $user_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; YB/5.1.3.0; DepositFiles/FileManager 0.9.9.206)";
      curl_setopt($ch, CURLOPT_USERAGENT, $user_agent );

      $mr = $maxredirect === null ? 5 : intval($maxredirect);

      if (ini_get('open_basedir') == '' && ini_get('safe_mode') == 'Off') {

        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0);
        curl_setopt($ch, CURLOPT_MAXREDIRS, $mr);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

      } else {

        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);

        if ($mr > 0)
        {
          $original_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
          $newurl = $original_url;

          $rch = curl_copy_handle($ch);

          curl_setopt($rch, CURLOPT_HEADER, true);
          curl_setopt($rch, CURLOPT_NOBODY, true);
          curl_setopt($rch, CURLOPT_FORBID_REUSE, false);
          do
          {
            curl_setopt($rch, CURLOPT_URL, $newurl);
            $header = curl_exec($rch);
            if (curl_errno($rch)) {
              $code = 0;
            } else {
              $code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
              if ($code == 301 || $code == 302) {
                preg_match('/Location:(.*?)\n/', $header, $matches);
                $newurl = trim(array_pop($matches));

                // if no scheme is present then the new url is a
                // relative path and thus needs some extra care
                if(!preg_match("/^https?:/i", $newurl)){
                  $newurl = $original_url . $newurl;
                }   
              } else {
                $code = 0;
              }
            }
          } while ($code && --$mr);

          curl_close($rch);

          if (!$mr)
          {
            if ($maxredirect === null)
            trigger_error('Too many redirects.', E_USER_WARNING);
            else
            $maxredirect = 0;

            return false;
          }
          curl_setopt($ch, CURLOPT_URL, $newurl);
        }
      }
      return curl_exec($ch);
    }

?>

消息来源:

http://slopjong.de/2012/03/31/curl-follow-locations-with-safe_mode-enabled-or-open_basedir-set/

答案 2 :(得分:-1)

  1. 我认为您需要添加RETURNTRANSFER以将结果传递给&#39; $ result&#39;

    curl_setopt($ ch,CURLOPT_RETURNTRANSFER,TRUE);

  2. 必须通过设置COOKIESESSION启用Cookie:

    curl_setopt($ ch,CURLOPT_COOKIESESSION,TRUE);