Jump to content
happycode

[Resolvido] Cron Job com ficheiro XML

Recommended Posts

happycode
function ftp_file_2501_import() {
	global $wpdb;

	/* Source File Name and Path */
	$remote_file = '/files/products/xml/standard/product_2501_pt.xml';
	 
	/* FTP Account */
	$ftp_host = 'xxxxx'; /* host */
	$ftp_user_name = 'xxxxx'; /* username */
	$ftp_user_pass = 'xxxx'; /* password */
	 
	 
	/* New file name and path for this file */
	$local_file = 'wp-content/folder/product_2501_pt.xml';
	 
	/* Connect using basic FTP */
	$connect_it = ftp_connect( $ftp_host );
	 
	/* Login to FTP */
	$login_result = ftp_login( $connect_it, $ftp_user_name, $ftp_user_pass );
	 
	/* Download $remote_file and save to $local_file */
	if ( ftp_get( $connect_it, $local_file, $remote_file, FTP_BINARY ) ) {
	    $xml = simplexml_load_file("https://www.xxxxxx.pt/wp-content/folder/product_2501_pt.xml");
	}

	 
	/* Close the connection */
	ftp_close( $connect_it );

    foreach($xml->children() as $product)
    {
    	$category = $product->category;
    	if (strpos($category, '2548') !== false) {
    		$id 			= $product->id;
    		$name 			= $product->name;
    		$description 	= $product->description;
    		$price 			= $product->price;
    		$width 			= $product->width;
    		$height 		= $product->height;
    		$depth 			= $product->depth;
    		$weight 		= $product->weight;
    		$stock 			= $product->stock;
    		$date_add 		= $product->date_add;
    		$date_upd 		= $product->date_upd;
    		$image1 		= $product->image1;
    		$image2 		= $product->image2;
    		$image3 		= $product->image3;
    		$image4 		= $product->image4;
    		$image5 		= $product->image5;
    		
		    $wpdb->query( "INSERT INTO wp6t_posts
				( post_type, post_author, post_status, post_title, post_content, post_date, post_modified )
				VALUES (product, 1 , draft, $name, $description, $date_add, $date_upd )"
			);
		}
    }
}

add_action( 'file_2501_import', 'ftp_file_2501_import');

 

Erro:

Warning: ftp_get(wp-content/folder/product_2501_pt.xml): failed to open stream: No such file or directory in /home/xxxx/public_html/wp-content/themes/theme-child/functions.php on line 28

Warning: ftp_get(): Error opening wp-content/folder/product_2501_pt.xml in /home/xxxxx/public_html/wp-content/themes/theme-child/functions.php on line 28

 

 

Bom dia malta,

Tenho estado a tentar a fazer download de um xml de um server para outro e posteriormente aceder ao xml e extrair o conteúdo e salvar na base de dados com recurso ao cron job para chamar a função diariamente.

Se meter a função a executar ao init, corre tudo bem, mas se usar o cron job dá problema na localização do xml.

Alguma dica?

 

Cumprimentos

Share this post


Link to post
Share on other sites
tiago.f

Olá Rafael, 

quando é o cron a iniciar o teu programa (por oposição a seres tu manualmente, ou o teu webserver), o programa é iniciado num contexto diferente. 

Por exemplo, e o mais provável, é que o cron corra o programa como root e a partir de um directório diferente, dai que paths como as que tens hardcoded, que são relativas, possam não funcionar quando o programa corre “pelo cron”. 

A sugestão é utilizares sempre paths absolutas - podes/deves obviamente construí-las com base em variáveis tipo __FILE__ ou __DIR__. 

 

EDIT: já agora na parte

simplexml_load_file("https://www.xxxxxx.pt/wp-content/folder/product_2501_pt.xml"); 

nao deverias usar $local_file em vez do URL? Senao pra quê o ftp_get ?

finalmente ... sacas um ficheiro por FTP e depois tens o simplexml a let um por https....estranho 

Edited by tiago.f

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.