Команда Cron Job не работает, когда я вызываю скрипт PHP для чтения RSS? - PullRequest
0 голосов
/ 30 августа 2018

Моя проблема возникает, когда я вызываю этот файл readXML.php из задания cron в cPanel (это не работает).

Я попробовал несколько команд:

/usr/local/bin/php -q /home/myUserName/public_html/readXML.php
/usr/local/bin/php /home/myUserName/public_html/readXML.php
php -q /home/myUserName/public_html/readXML.php
php /home/myUserName/public_html/readXML.php

Но команды не работают (они ничего не делают).

Мой скрипт работает на 100%, когда я вызываю их с URL:

(www.mysite.com/readXML.php)

Но я хочу запускать этот файл автоматически каждые 10 минут.

* / 10 * * * * А что за команда?

Cron Job в cPanel

<?php

include 'init.php';

global $dataFetchAll, $sahaID, $sourID, $deptID, $cateID, $deptNAME, $cateNAME,
    $newsTITLE, $newsDESC, $newsLINK, $newsDATE, $newsIMAGE;

$allFeedLinks = GetAllFeedLinks();

try
{
    foreach ($allFeedLinks as $link)
    {    
       $url = $link['SourFEEDLINK']; 

        $sourID = 0;
        $sahaID = 0;
        $deptID = 0;
        $cateID = 0;
        $deptNAME = "";
        $cateNAME = ""; 

        getInfoSource("SourFEEDLINK", $url); 
        $sourID = $sourceID;

        if($sourceDeptID > 0) 
        {
            $deptID = $sourceDeptID;
            getInfoDept("DeptID", $deptID); 
        }

        if($sourceSahaID > 0)
        {
            $sahaID = $sourceSahaID;
        }            

        if(($methodXML == 0) || ($methodXML == 1))
        {
            if($methodXML == 0)
            {
                $lines_string=file_get_contents($url);
                $xml = new SimpleXMLElement($lines_string);   
            }

            else if($methodXML == 1)
            {
                $ch = curl_init($url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

                curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36');

                $feed = curl_exec($ch);
                $xml = new SimpleXMLElement($feed);
            }

            for ($i = 0; $i < 5; $i++)
            {
                $newsTITLE = ""; 
                $newsLINK = ""; 
                $newsDESC = ""; 
                $newsDATE = "";  
                $newsIMAGE = ""; 

                if((isset($xml->channel->item[$i]->title)) && (!empty($xml->channel->item[$i]->title)))
                {
                    $newsTITLE = $xml->channel->item[$i]->title;                         
                    $rowNews = CheckNews("NewsTITLE", $newsTITLE); 

                    if($rowNews <= 0)
                    {
                        if((isset($xml->channel->item[$i]->link)) && (!empty($xml->channel->item[$i]->link)))
                            $newsLINK = $xml->channel->item[$i]->link;

                        if((isset($xml->channel->item[$i]->description)) && (!empty($xml->channel->item[$i]->description)))
                        {
                            $newsDESC = $xml->channel->item[$i]->description;
                            $newsDESC = RemoveTags($newsDESC);  
                        }

                        if((isset($xml->channel->item[$i]->pubDate)) && (!empty($xml->channel->item[$i]->pubDate)))
                        {
                            $pDate = $xml->channel->item[$i]->pubDate; 
                            $stringDate = ModifyDateString($pDate);

                            date_default_timezone_set('Asia/Riyadh');

                            $xDate = date("Y-m-d H:i:s", strtotime($stringDate)); 

                            $dateInNum = strtotime($xDate);
                            $current_time = time(); 

                            $result = $current_time - $dateInNum;
                            if($result >= 0)
                                $newsDATE = $xDate;

                            else
                            {
                                date_default_timezone_set('Asia/Riyadh');
                                $date = explode(" ", $stringDate);
                                $day = $date[0];
                                $d = $date[1];
                                $month = $date[2];
                                $year = $date[3];
                                $theTime = $date[4];

                                $newsDATE = $day ." " .$d ." " .$month ." " .$year ." " .$theTime;
                                $newsDATE = date("Y-m-d H:i:s", strtotime($newsDATE)); 
                            }
                        }

                        if((isset($xml->channel->item[$i]->enclosure['url'])) && (!empty($xml->channel->item[$i]->enclosure['url'])))
                            $newsIMAGE = $xml->channel->item[$i]->enclosure['url'];

                        if($deptID == 0)
                        {
                            if((isset($xml->channel->item[$i]->category)) && (!empty($xml->channel->item[$i]->category)))
                                $cateNAME = $xml->channel->item[$i]->category;       

                            if((empty($cateNAME)) || ($cateNAME==""))
                                $cateNAME = "الاخبار";

                            $rowCate = VerifyCategory("CateNAME", $cateNAME);

                            if($rowCate <= 0)
                            {
                                InsertCatedgoryToDB(); 

                                getInfoCategory("CateNAME", $cateNAME);
                            }
                        } 

                        if((!empty($newsTITLE)) && (!empty($newsLINK)) && (!empty($newsDATE)))
                        {
                            InsertNewsToDB();   
                        }
                    } 
                } 
            } 
        }

        else
        {
            $xmlDoc = new DOMDocument();
            $xmlDoc->load($url);

            $x = $xmlDoc->getElementsByTagName('item');

            for ($i = 0; $i < 5; $i++)
            {
                $newsTITLE = ""; 
                $newsLINK = ""; 
                $newsDESC = ""; 
                $newsDATE = "";  
                $newsIMAGE = "";

                if(!empty($x->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue))
                {
                    $newsTITLE =$x->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
                    $rowNews = CheckNews("NewsTITLE", $newsTITLE); 

                    if($rowNews <= 0)
                    {
                        if(!empty($x->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue))
                            $newsLINK = $x->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;

                        if(!empty($x->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue))
                            $newsLINK = $x->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;

                        else if((empty($newsLINK)) && (!empty($x->item($i)->getElementsByTagName('link')->item(0)->attributes['href']->nodeValue)))
                            $newsLINK = $x->item($i)->getElementsByTagName('link')->item(0)->attributes['href']->nodeValue;

                        if(!empty($x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue))
                        {
                            $newsDESC = $x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
                            $newsDESC = RemoveTags($newsDESC);  
                        }

                        if((!empty($x->item($i)->getElementsByTagName('pubDate')->item(0)->childNodes->item(0)->nodeValue)))
                        {
                            $pDate = $x->item($i)->getElementsByTagName('pubDate')->item(0)->childNodes->item(0)->nodeValue;
                            $stringDate = ModifyDateString($pDate);
                            date_default_timezone_set('Asia/Riyadh');
                            $xDate = date("Y-m-d H:i:s", strtotime($stringDate)); 
                            $dateInNum = strtotime($xDate);
                            $current_time = time(); 

                            $result = $current_time - $dateInNum;
                            if($result >= 0)
                                $newsDATE = $xDate;

                            else
                            {
                                date_default_timezone_set('Asia/Riyadh');
                                $date = explode(" ", $stringDate);
                                $day = $date[0];
                                $d = $date[1];
                                $month = $date[2];
                                $year = $date[3];
                                $theTime = $date[4];

                                $newsDATE = $day ." " .$d ." " .$month ." " .$year ." " .$theTime; 
                                $newsDATE = date("Y-m-d H:i:s", strtotime($newsDATE));
                            }
                        }

                        if(!empty($x->item($i)->getElementsByTagName('enclosure')->item(0)->attributes['url']->nodeValue))
                            $newsIMAGE = $x->item($i)->getElementsByTagName('enclosure')->item(0)->attributes['url']->nodeValue;

                        if($deptID == 0)
                        {
                            if((!empty($x->item($i)->getElementsByTagName('category')->item(0)->childNodes->item(0)->nodeValue)))
                                $cateNAME = $x->item($i)->getElementsByTagName('category')->item(0)->childNodes->item(0)->nodeValue;       

                            if((empty($cateNAME)) || ($cateNAME==""))
                                $cateNAME = "الاخبار";

                            $rowCate = VerifyCategory("CateNAME", $cateNAME);

                            if($rowCate <= 0)
                            {
                                InsertCatedgoryToDB(); 
                                getInfoCategory("CateNAME", $cateNAME);
                            }
                        } 

                        if((!empty($newsTITLE)) && (!empty($newsLINK)) && (!empty($newsDATE)))
                        {
                             InsertNewsToDB();   
                        }
                    }
                }
            }
        } 

    } 
} 

catch(PDOException $e){
    echo $e->getMessage();
}
?>
...