【问题标题】:Curl does not have time to work out when you run through Cron跑完 Cron 时,Curl 没有时间锻炼
【发布时间】:2014-10-06 23:34:13
【问题描述】:

我有一个 PHP cron 脚本

Class Cron {
  public function __construct {
    $this->dosomething(); 
  } 
  private function dosomething () {
    $curl = app_curl(); 
  } 
} 

function app_curl () {
  ... $ Curl ... 
  return $ curl; 
} 

当我运行这个脚本时 - 它不满意,并中止了 Cron。如果我单独运行 app_curl(),那么一切正常。

// 完整代码

// cli/auto cron 文件(代码点火器)

class Auto extends CLI_Controller {
  public function index() {
    $this->process_broadcasts();
  } // function

  private function process_broadcasts(){
    $curl = app_curl_get_postmaster();
  } // function
} // class

// curl_function

function get_web_page($url, $post = array())
{
  if (!isset($url) || empty($url)) return false;
  $uagent = "Opera/9.80 (Windows NT 6.1; WOW64) Presto/2.12.388 Version/12.14";

  $ch = curl_init( $url );

  $path = dirname(__FILE__);
  $tmpfile = $path . DIRECTORY_SEPARATOR . 'cookie' . '.txt';

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);     // возвращает веб-страницу

  curl_setopt($ch, CURLOPT_HEADER, 0);             // не возвращает заголовки
  curl_setopt($ch, CURLOPT_ENCODING, "");          // обрабатывает все кодировки
  curl_setopt($ch, CURLOPT_USERAGENT, $uagent);    // useragent

  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);   // таймаут соединения
  curl_setopt($ch, CURLOPT_TIMEOUT, 120);          // таймаут ответа

  curl_setopt($ch, CURLOPT_COOKIESESSION, 1);     // переходит по редиректам
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);     // переходит по редиректам
  curl_setopt($ch, CURLOPT_MAXREDIRS, 10);         // останавливаться после 10-ого редиректа

  curl_setopt($ch, CURLOPT_COOKIEFILE,  $tmpfile);
  curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpfile);

  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

  if (isset($post) && !empty($post)) {
    if (!is_array($post)) $post[$$post] = $post;
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
  }

  $content = curl_exec( $ch );
  $err     = curl_errno( $ch );
  $errmsg  = curl_error( $ch );
  $header  = curl_getinfo( $ch );
  curl_close( $ch );

  $header['errno']   = $err;
  $header['errmsg']  = $errmsg;
  $header['content'] = $content;
  return $header;
}

// get_postmaster

function app_curl_get_postmaster($date = null) {
  if ($date === null) $date = date('d.m');

  $post = array(
    'Login' => 'gzhegow2@mail.ru',
    'Domain' => 'mail.ru',
    'Password' => '321qwe',
    'saveauth' => 1,
    'new_auth_form' => 1,
    'page' => 'https://postmaster.mail.ru/',
    'post' => '',
    'login_form' => ''
  );

  $v = get_web_page('https://auth.mail.ru/cgi-bin/auth', $post);
  $v = get_web_page('https://postmaster.mail.ru/100tr.ru/');

  $html = str_get_html($v['content']);

  $tmp = array();
  $arr = array();

  $tr_counter = 0;
  foreach ($html->find('table[id=stats] tr') as $tr) {
    foreach ($tr->find('td[class*=statistic-table__date]') as $td) {
      $tmp[$tr_counter]['date'] = trim($td->plaintext);
    }
    foreach ($tr->find('td[class*=statusbar__delivered]') as $td) {
      $tmp[$tr_counter]['good'] = trim($td->plaintext);
    }
    foreach ($tr->find('td[class*=statusbar__probspam]') as $td) {
      $tmp[$tr_counter]['warn'] = trim($td->plaintext);
    }
    foreach ($tr->find('td[class*=statusbar__spam]') as $td) {
      $tmp[$tr_counter]['spam'] = trim($td->plaintext);
    }
    $tr_counter++;
  };

  foreach ($tmp as $item) {
    if (isset($item['date']) && !empty($item['date'])) $arr[$item['date']] = $item;
  }

  return $arr[$date];
}

我认为 Curl 在通过另一个脚本运行时没有时间执行,但如果我这样做了

print_r(app_curl_get_postmaster());

然后一切正常。可能是什么原因?

【问题讨论】:

  • maybe this...? 零是无限的,也略过 cmets..
  • 您是否包含/需要文件,如果是,您可能需要使用完整的服务器路径来文件
  • 也许,我现在测试一下...更多变种?
  • 约翰尼,是的,function_exists 返回 true
  • 我的Skype是gzhegow,我可以显示我的屏幕

标签: php codeigniter curl cron


【解决方案1】:

哈哈,我用 ::

error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('max_execution_time', 3600);
ini_set('memory_limit', '256M');

他们告诉我“str_get_html”是“未知函数”。 我确实需要 simple_html_dom.php 并且它有效。

感谢大家的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多