1

我可以通过 URL 直接访问这个脚本,它工作正常,但作为一个 cron 作业 ot 不起作用。是 aweber 还是我做错了什么?

Awebre 的文档是我遇到过的最糟糕的文档之一!

我不确定为什么他们的文档中没有对此进行解释!

谢谢

<?php 
include "wp-load.php";
include_once('wp-includes/class-phpass.php');
$sql = "SELECT member_id, email FROM wp_members_tbl WHERE aweber != 1";
$result = $wpdb->get_results($sql);

if(count($result)>0)
	{
		##Add aweber
		require_once('aweber/aweber_api/aweber_api.php');
		$consumerKey = '***';
		$consumerSecret = '***';
		$accessKey      = '***'; # put your credentials here
		$accessSecret   = '***'; # put your credentials here
		$account_id     = '***'; # put the Account ID here
		$list_id        = '***'; # put the List ID here 3823593   
		$aweber = new AWeberAPI($consumerKey, $consumerSecret);
		# Get an access token
		if(empty($_COOKIE['accessToken']))
		    {
		        if (empty($_GET['oauth_token'])) 
		            {
		                $callbackUrl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		                list($requestToken, $requestTokenSecret) = $aweber->getRequestToken($callbackUrl);
		                setcookie('requestTokenSecret', $requestTokenSecret);
		                setcookie('callbackUrl', $callbackUrl);
		                header("Location: {$aweber->getAuthorizeUrl()}");
		                exit();
		            }
		        $aweber->user->tokenSecret = $_COOKIE['requestTokenSecret'];
		        $aweber->user->requestToken = $_GET['oauth_token'];
		        $aweber->user->verifier = $_GET['oauth_verifier'];
		        list($accessToken, $accessTokenSecret) = $aweber->getAccessToken();
		        setcookie('accessToken', $accessToken);
		        setcookie('accessTokenSecret', $accessTokenSecret);
		        header('Location: '.$_COOKIE['callbackUrl']);
		        exit();
			}
		##End add aweber

		foreach($result as $val=>$row)
			{
				# Get AWeber Account
					try {
					        $account = $aweber->getAccount($_COOKIE['accessToken'], $_COOKIE['accessTokenSecret']);
					        $listURL = "https://api.aweber.com/1.0/accounts/***/lists/".$list_id;
					        $list = $account->loadFromUrl($listURL);
							$params = array(
					            'email' => $row->email
					        );
					        $subscribers = $list->subscribers;
					        $new_subscriber = $subscribers->create($params);
							$update_data = array('aweber' => 1);
							$where = array('member_id' => $row->member_id);
							$wpdb->update( 'wp_members_tbl', $update_data, $where, $format = null, $where_format = null);
					        
					       					        # success!
					        //print "A new subscriber was added to the $list->name list!";
					    }
					catch(AWeberAPIException $exc) 
					    {
					        print "<h3>AWeberAPIException:</h3>";
					        print " <li> Type: $exc->type              <br>";
					        print " <li> Msg : $exc->message           <br>";
					        print " <li> Docs: $exc->documentation_url <br>";
					        print "<hr>";
					        //exit(1);
					    }
			}
	}

4

2 回答 2

0

看起来有几个地方可能会失败:

1)您的包含中有相对路径。cron 可能无法找到包含的文件。尝试将这些更改为完整路径。2) 您的代码需要浏览器环境(设置 cookie、http 标头位置等)。这些都不适用于浏览器之外。

您应该始终从命令行执行该文件作为 cron 运行的用户来测试它。任何错误都会很明显。

要解决您的问题,您应该使用本教程,该教程使用浏览器设置凭据。之后,它可以从命令行/cron 运行: http ://engineering.aweber.com/quick-start-api-script-in-php/

于 2015-04-22T23:00:19.340 回答
0

尝试在 cron 命令中使用它:

wget -qO- http://yoururlhere/ &> /dev/null

这不是最好的解决方案,因为不建议使用“自调用”,但它会让您无法处理环境设置。

于 2015-09-08T07:22:30.787 回答