php cron工作问题?

时间:2012-04-16 10:38:19

标签: php cron

我使用php代码使用cron来表示代码。我也在cpanel中设置了cron时间和命令。

1)。但每当cron运行我收到邮件

/home/letsview/public_html/getfeed.php: line 1: ?php: No such file or directory
/home/letsview/public_html/getfeed.php: line 3: syntax error near unexpected token `'/home/letsview/public_html/wp-config.php''
/home/letsview/public_html/getfeed.php: line 3: `include_once('/home/letsview/public_html/wp-config.php');'

我在cpanel "/home/letsview/public_html/getfeed.php"

中设置了此命令

我也试过这个PHP: Error trying to run a script via Cron job并在文件/usr/local/lib/php/的顶部添加了此命令,但它仍然无法正常工作

以下是cron文件getfeed.php

的代码
<?php
#!/usr/local/lib/php/
include_once('/home/letsview/public_html/wp-config.php');
include_once('/home/letsview/public_html/wp-includes/wp-db.php');
include_once('/home/letsview/public_html/wp-admin/includes/file.php');          
include_once('/home/letsview/public_html/wp-admin/includes/image.php');         
include_once('/home/letsview/public_html/wp-admin/includes/media.php');
global $wpdb;
 //property_type
$xml = simplexml_load_file("/home/letsview/public_html/letsviewproperties.xml",'SimpleXMLElement', LIBXML_NOCDATA);
$TotalPostadded = 0;
$TotalUseradded = 0;
foreach($xml as $child)
{
    //Insert Post
    $postdata = array();
    $postdata['post_title'] = trim($child->title);
    $postdata['post_content'] = trim($child->content);
    //$postdata['guid'] = trim($child->url);
    $postdata['post_status'] = 'publish';
    $postdata['post_type'] = 'post';
    $postdata['post_date'] = date('Y-m-d H:i:s');

    //Insert Post Meta
    $postmetadata = array();
    $addresstext = trim($child->FullAddress->address1);
    if($addresstext != ''){
        $addresstext .= ', ';
    }
    $addresstext .= trim($child->FullAddress->address2);
    if($addresstext != ''){
        $addresstext .= ', ';
    }
    $addresstext .= trim($child->FullAddress->address3);
    if($addresstext != ''){
        $addresstext .= ', ';
    }
    $addresstext .= trim($child->FullAddress->address4);
    $postmetadata['price'] = trim($child->price);
    $postmetadata['property_type'] = trim($child->type);
    $postmetadata['bed_rooms'] = trim($child->rooms);
    $postmetadata['bath_rooms'] = trim($child->bathrooms);
    $postmetadata['address'] = $addresstext;
    $postmetadata['add_city'] = trim($child->city);
    $postmetadata['add_state'] = trim($child->FullAddress->region);
    $postmetadata['add_country'] = trim($child->FullAddress->country);
    $postmetadata['add_zip_code'] = trim($child->postcode);
    $postmetadata['other_guid'] = trim($child->url);
    $postmetadata['post_from_feed'] = true;

    //Insert Author(agent)
    $authordata = array();
    $authormetadata = array();
    if(!empty($child->agent->agent_name)){
        //Author data
        $authordata['user_login'] = trim(pg_create_string($child->agent->agent_name));
        $authordata['user_nicename'] = trim($child->agent->agent_name);
        $authordata['display_name'] = trim($child->agent->agent_name);
        $authordata['user_email'] = trim($child->agent->agent_email);
        $authordata['user_url'] = trim($child->url);
        $authordata['role'] = trim('agent');
        $authordata['user_registered'] = date('Y-m-d H:i:s');
        //Author meta data
        $authormetadata['user_phone'] = trim($child->agent->agent_phone);
        $authormetadata['user_address'] = trim($child->agent->agent_address);
    }
    foreach($child->pictures as $pictures)
    {
        $postimagedata = array();
        $imageloop = 0;
        foreach($pictures as $picture)
        {
            $postimagedata[$imageloop] = (string)$picture->picture_url;
            $imageloop++;
        }
    }
    $postmetadata['post_from_feed_images'] = serialize($postimagedata);
    if($postdata['post_title'] != ''){
        $sql = "select count(post_title) as post from ".$wpdb->prefix."posts  where post_title = '".$postdata['post_title']."' and post_status = '".$postdata['post_status']."'";
        $sqlresult = $wpdb->get_results($sql);
        foreach ( $sqlresult as $post ) { 
            if($post->post == 0)
            {
                if(!empty($authordata)){
                    $user_id = wp_insert_user( $authordata );

                    if(!empty($user_id) && empty($user_id->errors)){
                        $TotalUseradded++;
                        echo "User added = ".$user_id."<br />";
                        if(!empty($authormetadata)){
                            foreach($authormetadata as $meta_key=>$meta_value){
                                add_user_meta( $user_id, $meta_key, $meta_value);
                                echo "User Meta = ".$meta_key." Inserted<br />";
                            }
                        }
                    }elseif(!empty($user_id->errors)){
                        $userdata = get_user_by('email', $authordata['user_email']);
                        $user_id = $userdata->ID;
                        echo "User fetched = ".$user_id."<br />";
                    }
                    $postdata['post_author'] = $user_id;
                }
                $post_id = wp_insert_post($postdata);
                if(!empty($post_id)){
                    $TotalPostadded++;  
                    echo "<br />"."Post Inserted = ".$post_id;
                    $properties_category_id = 109;
                    $cat = "INSERT INTO wp_term_relationships ( object_id, term_taxonomy_id ) VALUES ( '".$post_id."','".$properties_category_id."' )";
                    $catid = $wpdb->query($cat);
                    echo "<br />"."Post attached to Category ID = ".$properties_category_id."<br />";
                    if(!empty($postmetadata)){
                        foreach($postmetadata as $key=>$value){
                            add_post_meta($post_id, $key,$value, true);
                            echo "Post Meta = ".$key." Inserted<br />";
                        }
                    }
                }   
            }
        }
    }
}
$cron = "<br />"."Corn Done";
$cron .= "<br />"."Total Post added = ".$TotalPostadded;
$cron .= "<br />Total User added = ".$TotalUseradded;
echo $cron;
mail('xxxxxx@xxxxx.com','Lets view Properties Corn',$cron);
function pg_create_string($text)
{
  // replace all non letters or digits with -
  $text = preg_replace('/\W+/', '-', $text);

  // trim and lowercase
  $text = strtolower(trim($text, '-'));
  return $text;
} 
?>

任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:2)

文件的开头应为:

#!/usr/bin/php
<?php

这假设您的PHP二进制文件位于/ usr / bin文件夹中。如果不是,那么改变#!适当地排队。

更好:

#!/usr/bin/env php
<?php

几乎肯定会工作,因为它使用系统的env命令来确定php的位置。

答案 1 :(得分:1)

将其添加到文件的最顶层,然后chmod该文件以执行权限(555或775)等。

#!/usr/local/lib/php
<?php
// your code

其中/ usr / local / lib / php是php的路径。

如果这不起作用,您可以更改cron命令:

/usr/local/lib/php /home/letsview/public_html/getfeed.php

答案 2 :(得分:0)

#!/usr/local/lib/php
<?php
// php code

你确定/ clr / local / lib中的php cli