部分 PHP 脚本无法在 EXEC 中运行?
我有一个在后台运行的 PHP 脚本,用于将任何图片发布到他们的社交网站或通过电子邮件发送。所有数据都存储在数据库中,并且确实会像这样调用运行:
$user_id = "77";
exec("/usr/bin/php5 upload_auto.php?user_id=$user_id");
这是发布代码的样子:
<?php
$the_picture_id_to_check = $_GET['user_id'];
mysql_connect("localhost","********", "********") or die(mysql_error());
mysql_select_db(myflashp_pictures) or die (mysql_error());
require_once('connect/twitter_files/twitteroauth/twitteroauth.php');
require_once('connect/twitter_files/config.php');
?>
<!--- Upload Tumblr --->
<?php
$result_tumblr = mysql_query("SELECT * FROM upload_tumblr WHERE user_id=$the_picture_id_to_check");
$num_rows_find = mysql_num_rows($result_tumblr);
while($row_tumblr=mysql_fetch_array($result_tumblr)){
// Get Username
$the_id = "".$row_tumblr['id']."";
$picture_id = "".$row_tumblr['picture_id']."";
$picture_url = "".$row_tumblr['picture_url']."";
$user_id = "".$row_tumblr['user_id']."";
$caption = "".$row_tumblr['caption']."";
$album_id = "".$row_tumblr['album_id']."";
// Authorization info
$monday_result = mysql_query("SELECT * FROM photo_albums WHERE id='$album_id'");
$monday_query_row=mysql_fetch_array($monday_result);
$tumblr_id = $monday_query_row['tumblr_id'];
$monday_result2 = mysql_query("SELECT * FROM connections_tumblr WHERE id='$tumblr_id'");
$monday_query_row2 = mysql_fetch_array($monday_result2);
$tumblr_email = $monday_query_row2['email'];
$tumblr_password = $monday_query_row2['password'];
// Prepare POST request
$request_data = http_build_query(
array(
'email' => $tumblr_email,
'password' => $tumblr_password,
'type' => 'photo',
'source' => $picture_url,
'caption' => $caption,
'generator' => 'FlashPics iPhone App'
)
);
// Send the POST request (with cURL)
$c = curl_init('http://www.tumblr.com/api/write');
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $request_data);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($c);
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
// Check for success
if ($status == 201) {
mysql_query("UPDATE pictures SET tumblr_id='$result' WHERE id='$picture_id'");
echo "Success! The new Tumblr post ID is $result.<br>\n";
mysql_query("DELETE FROM upload_tumblr WHERE id='$the_id'");
} else if ($status == 403) {
echo 'Bad email or password for Tumblr.';
} else if ($status == 400) {
echo "Error ($status): $result\n";
mysql_query("DELETE FROM upload_tumblr WHERE id='$the_id'");
} else {
echo "Error ($status): $result\n";
}
}
if ($num_rows_find == 0) {
echo "There's nothing to post to Tumblr.";
}
?>
<br /><br />
<!--- Upload Email --->
<?php
include_once('class.phpmailer.php');
$result_email = mysql_query("SELECT * FROM upload_email WHERE status='processing' AND user_id='$the_picture_id_to_check'");
$num_rows_find = mysql_num_rows($result_email);
mysql_query("UPDATE upload_email SET status='$num_rows_find' WHERE id='1'");
while($row_email=mysql_fetch_array($result_email)){
// Get Username
$to = "".$row_email['email']."";
$get_info_result = mysql_query("SELECT * FROM users WHERE id='".$row_email['user_id']."'");
$get_info_query_row = mysql_fetch_array($get_info_result);
$get_username = $get_info_query_row['username'];
$get_first_name = $get_info_query_row['first_name'];
$get_last_name = $get_info_query_row['last_name'];
$get_info_result = mysql_query("SELECT * FROM pictures WHERE id='".$row_email['picture_id']."'");
$get_info_query_row = mysql_fetch_array($get_info_result);
$get_caption = $get_info_query_row['caption'];
$get_picture_url = $get_info_query_row['long_url'];
$get_short_string = $get_info_query_row['short_string'];
if ($get_caption == "") {
$get_caption = "I have shared a FlashPic with you.";
}
$mail = new PHPMailer();
$mail->From = "[email protected]";
$mail->FromName = "FlashPics";
$mail->Subject = "$get_first_name $get_last_name has shared a FlashPic with you.";
$mail->IsHTML(true);
$mail->Body = "
<html><body style='margin: 0px; padding: 0px;'>
<div style='padding: 15px; width: 480px; font-family: Helvetica; margin: 10px; border: 1px solid #d7d7d7;'>
<div style='font-weight: bolder; font-size: 32px; margin: 8px 0px 20px 0px;'><font color='#353535'>Flash</font><font color='#f8bc49'>Pics</font></div>
<div style='font-size: 20px; color: #353535; font-weight: normal; text-transform: lowercase;'>$get_username</div>
<div style='margin-top: 15px; font-size: 14px; line-height: 18px;'>$get_caption<br>
<br>
<img src=$get_picture_url width='480'>
<br><br>
<a href='http://myflashpics.com/picture/$get_short_string' style='color: #000000; text-decoration: underline;'>view full size image</a><br>
<br>
Sincerely,<br>
The FlashPics Team</div>
</div>
</body></html>";
$mail->AltBody = "$get_username
$get_caption
http://myflashpics.com/picture/".$row_email['picture_id']."
Yours Truly,
The FlashPics Team";
$the_id = "".$row_email['id']."";
$pieces = explode(", ", "$to");
$one = $pieces['0'];
$two = $pieces['1'];
$three = $pieces['2'];
$four = $pieces['3'];
$five = $pieces['4'];
if ($one != "") {
$mail->AddBCC("$one");
}
if ($two != "") {
$mail->AddBCC("$two");
}
if ($three != "") {
$mail->AddBCC("$three");
}
if ($four != "") {
$mail->AddBCC("$four");
}
if ($five != "") {
$mail->AddBCC("$five");
}
if(!$mail->Send()) {
echo "Failed sending email to <b> $one, $two, $three, $four, $five</b>." . $mail->ErrorInfo;
mysql_query("UPDATE upload_email SET status='error' WHERE id='$the_id'");
} else {
echo "Sent email to <b> ".$row_email['email']."</b> successfully.<br>";
mysql_query("UPDATE upload_email SET status='sent' WHERE id='$the_id'");
}
}
if ($num_rows_find == 0) {
echo "There are no pictures to email out.";
}
?>
我的问题是我可以从浏览器运行它,没有问题,但是如果通过 PHP exec 运行它,则什么也没有发生。另一个奇怪的部分是某些操作正在发挥作用(例如 Facebook 和 Twitter)。
预先感谢,
库尔顿
I have a PHP script that runs in the background to post any pictures to their social networking sites or email it out. All the data is stored in the database and it does get called to run like this:
$user_id = "77";
exec("/usr/bin/php5 upload_auto.php?user_id=$user_id");
And here's what the posting code looks like:
<?php
$the_picture_id_to_check = $_GET['user_id'];
mysql_connect("localhost","********", "********") or die(mysql_error());
mysql_select_db(myflashp_pictures) or die (mysql_error());
require_once('connect/twitter_files/twitteroauth/twitteroauth.php');
require_once('connect/twitter_files/config.php');
?>
<!--- Upload Tumblr --->
<?php
$result_tumblr = mysql_query("SELECT * FROM upload_tumblr WHERE user_id=$the_picture_id_to_check");
$num_rows_find = mysql_num_rows($result_tumblr);
while($row_tumblr=mysql_fetch_array($result_tumblr)){
// Get Username
$the_id = "".$row_tumblr['id']."";
$picture_id = "".$row_tumblr['picture_id']."";
$picture_url = "".$row_tumblr['picture_url']."";
$user_id = "".$row_tumblr['user_id']."";
$caption = "".$row_tumblr['caption']."";
$album_id = "".$row_tumblr['album_id']."";
// Authorization info
$monday_result = mysql_query("SELECT * FROM photo_albums WHERE id='$album_id'");
$monday_query_row=mysql_fetch_array($monday_result);
$tumblr_id = $monday_query_row['tumblr_id'];
$monday_result2 = mysql_query("SELECT * FROM connections_tumblr WHERE id='$tumblr_id'");
$monday_query_row2 = mysql_fetch_array($monday_result2);
$tumblr_email = $monday_query_row2['email'];
$tumblr_password = $monday_query_row2['password'];
// Prepare POST request
$request_data = http_build_query(
array(
'email' => $tumblr_email,
'password' => $tumblr_password,
'type' => 'photo',
'source' => $picture_url,
'caption' => $caption,
'generator' => 'FlashPics iPhone App'
)
);
// Send the POST request (with cURL)
$c = curl_init('http://www.tumblr.com/api/write');
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $request_data);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($c);
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
// Check for success
if ($status == 201) {
mysql_query("UPDATE pictures SET tumblr_id='$result' WHERE id='$picture_id'");
echo "Success! The new Tumblr post ID is $result.<br>\n";
mysql_query("DELETE FROM upload_tumblr WHERE id='$the_id'");
} else if ($status == 403) {
echo 'Bad email or password for Tumblr.';
} else if ($status == 400) {
echo "Error ($status): $result\n";
mysql_query("DELETE FROM upload_tumblr WHERE id='$the_id'");
} else {
echo "Error ($status): $result\n";
}
}
if ($num_rows_find == 0) {
echo "There's nothing to post to Tumblr.";
}
?>
<br /><br />
<!--- Upload Email --->
<?php
include_once('class.phpmailer.php');
$result_email = mysql_query("SELECT * FROM upload_email WHERE status='processing' AND user_id='$the_picture_id_to_check'");
$num_rows_find = mysql_num_rows($result_email);
mysql_query("UPDATE upload_email SET status='$num_rows_find' WHERE id='1'");
while($row_email=mysql_fetch_array($result_email)){
// Get Username
$to = "".$row_email['email']."";
$get_info_result = mysql_query("SELECT * FROM users WHERE id='".$row_email['user_id']."'");
$get_info_query_row = mysql_fetch_array($get_info_result);
$get_username = $get_info_query_row['username'];
$get_first_name = $get_info_query_row['first_name'];
$get_last_name = $get_info_query_row['last_name'];
$get_info_result = mysql_query("SELECT * FROM pictures WHERE id='".$row_email['picture_id']."'");
$get_info_query_row = mysql_fetch_array($get_info_result);
$get_caption = $get_info_query_row['caption'];
$get_picture_url = $get_info_query_row['long_url'];
$get_short_string = $get_info_query_row['short_string'];
if ($get_caption == "") {
$get_caption = "I have shared a FlashPic with you.";
}
$mail = new PHPMailer();
$mail->From = "[email protected]";
$mail->FromName = "FlashPics";
$mail->Subject = "$get_first_name $get_last_name has shared a FlashPic with you.";
$mail->IsHTML(true);
$mail->Body = "
<html><body style='margin: 0px; padding: 0px;'>
<div style='padding: 15px; width: 480px; font-family: Helvetica; margin: 10px; border: 1px solid #d7d7d7;'>
<div style='font-weight: bolder; font-size: 32px; margin: 8px 0px 20px 0px;'><font color='#353535'>Flash</font><font color='#f8bc49'>Pics</font></div>
<div style='font-size: 20px; color: #353535; font-weight: normal; text-transform: lowercase;'>$get_username</div>
<div style='margin-top: 15px; font-size: 14px; line-height: 18px;'>$get_caption<br>
<br>
<img src=$get_picture_url width='480'>
<br><br>
<a href='http://myflashpics.com/picture/$get_short_string' style='color: #000000; text-decoration: underline;'>view full size image</a><br>
<br>
Sincerely,<br>
The FlashPics Team</div>
</div>
</body></html>";
$mail->AltBody = "$get_username
$get_caption
http://myflashpics.com/picture/".$row_email['picture_id']."
Yours Truly,
The FlashPics Team";
$the_id = "".$row_email['id']."";
$pieces = explode(", ", "$to");
$one = $pieces['0'];
$two = $pieces['1'];
$three = $pieces['2'];
$four = $pieces['3'];
$five = $pieces['4'];
if ($one != "") {
$mail->AddBCC("$one");
}
if ($two != "") {
$mail->AddBCC("$two");
}
if ($three != "") {
$mail->AddBCC("$three");
}
if ($four != "") {
$mail->AddBCC("$four");
}
if ($five != "") {
$mail->AddBCC("$five");
}
if(!$mail->Send()) {
echo "Failed sending email to <b> $one, $two, $three, $four, $five</b>." . $mail->ErrorInfo;
mysql_query("UPDATE upload_email SET status='error' WHERE id='$the_id'");
} else {
echo "Sent email to <b> ".$row_email['email']."</b> successfully.<br>";
mysql_query("UPDATE upload_email SET status='sent' WHERE id='$the_id'");
}
}
if ($num_rows_find == 0) {
echo "There are no pictures to email out.";
}
?>
My problem is that I can run it from the browser no problem, however if run it via PHP exec, nothing happens. Another wierd part is that some actions are working (such as Facebook and twitter).
Thanks in advance,
Coulton
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用
CURL
代替:我发现从命令行运行 PHP 有时会带来意想不到的结果。
CURL
将像浏览器一样运行它。Try using
CURL
instead:I've found that running PHP from the command line sometimes brings unexpected results.
CURL
will run it like a browser would.我遇到了类似的问题,其中 exec() 或 shell_exec() 都不适用于我的脚本之一。
我最终使用了 system()
尝试
system("/usr/bin/php5 upload_auto.php?user_id=$user_id /dev/null 2>&1");
I had a similar problem where neither exec() or shell_exec() worked on one of my scripts.
I ended up using system()
try
system("/usr/bin/php5 upload_auto.php?user_id=$user_id /dev/null 2>&1");
编写类,其中将包含upload_auto.php的主要代码。并调用此类的方法,而不是使用“exec”。
Write class, which will contain main code of upload_auto.php. And call method of this class, instead of using "exec".