2012-04-16 63 views
0

我使用php代碼來執行使用cron的代碼。我也在cPanel中設置了cron時間和命令。php 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('[email protected]','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; 
} 
?> 

可以在任何一個H的代碼elp我??

回答

1

的文件開始應該是:

#!/usr/bin/php 
<?php 

這假設您的PHP二進制文件位於文件夾/ usr/bin中。如果不是,則更改#!適當地行。

更妙的是:

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

幾乎肯定會工作,因爲它使用系統的env命令到哪裏工作PHP是。

+0

如何檢查php二進制文件的位置? – 2012-04-16 10:45:19

+0

如果你有服務器的命令行訪問權限,那麼輸入'which php'。如果不是,那麼在使用/ usr/bin/env的答案中嘗試更新後的版本,以確定在哪裏安裝了php。 – 2012-04-16 10:58:13

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 
0
#!/usr/local/lib/php 
<?php 
// php code 

而且你確保PHP CLI在/ usr/local/lib目錄

+0

如何檢查php cli是否存在? – 2012-04-16 10:43:07