Jump to content

PHP script to export for Facebook data feed CSV


ludwig
Go to solution Solved by Barand,

Recommended Posts

Hello,

I created a PHP script to export my catalog on a ecommerce website on a CSV file and use it to feed Facebook catalog. But Facebook catalog can't read the columns:

facebook-error.thumb.jpg.7330a78e9579cfd85d5c997e088d453f.jpg

But if I open the csv file on Excel, do nothing, just Save it and use it again on Facebook, then Facebook accept it, and all data are transferred !!

This is the PHP script:

define('FEEDNAME', 'catalog_products.csv');
$OutFile = "feeds/" . FEEDNAME;

$output = "id,title,description,availability,condition,price,link,image_link,brand,google_product_category,fb_product_category,quantity_to_sell_on_facebook,sale_price,sale_price_effective_date,item_group_id,gender,color,size,age_group,material,pattern,shipping,shipping_weight,style";
$attributesColumns = array();

$output .= "\n";
	   
$output .= $row->id . ",";
$output .= $productTitle . ",";
$output .= 'Visit our website for more info.' . ",";
$output .= $availability . ",";
$output .= 'New' . ",";
$output .= $row->price . ",";
$output .= $row->image_url . ",";      
$output .= $row->brand . ",";    
$output .= $row->catName . ",";
$output .= $row->catName . ",";
$output .= $row->quantity . ",";
$output .= ",";
$output .= ",";
$output .= ",";
$output .= ",";
$output .= ",";
$output .= ",";
$output .= ",";
$output .= ",";
$output .= ",";
$output .= ",";
$output .= ",";
$output .= ",";

$output .= " \n";

$fp = fopen( $OutFile , "a" );
$fout = fwrite( $fp , $output );
fclose( $fp );

Maybe I should add some headers to the export file ? I'm not sure how to handle this

Any help please ?

Cheers :

Link to comment
Share on other sites

The other obvious problem with your code is that it appears to be based on a database query result set, but you didn't include that in your code, nor is there a "foreach" loop over the result set building up the csv file with the results.  So while you may or may not need the csv header row, your code as presented has no hope of actually working, without the availability of the data in a $row variable that is fetched from the result set in a loop.  

Another issue you need to be aware of is that strings should always be delimited with double quotes around them.  So you need to be cognizant of which columns are strings, and also you need to make sure that any embedded double quotes are escaped.

So for example, these lines are improper:

$output .= $productTitle . ",";
$output .= 'Visit our website for more info.' . ",";

That needs to be this:

$output .= $productTitle . ",";
$output .= '"Visit our website for more info."' . ",";

These problems are already solved for you in the function requinix linked you to.

Link to comment
Share on other sites

@gizmola Thank you and thanks for all for the help.

I minimized my script to this:

<?php

define( 'FEEDNAME', 'catalog_products.csv' ); 

$OutFile = "feeds/" . FEEDNAME;
$destination_file = FEEDNAME;
$source_file = $OutFile;

if ( file_exists( $OutFile ) ) {
    unlink( $OutFile );
}

$output = "id,title,description,availability,condition,price,link,image_link,brand,google_product_category,fb_product_category,quantity_to_sell_on_facebook,sale_price,sale_price_effective_date,item_group_id,gender,color,size,age_group,material,pattern,shipping,shipping_weight,style";

$output .= "\n";

$output .= "Test1,Test2,Test3,Test4,Test5,Test6,Test7,Test8,Test9,Test10,Test11,Test12,Test13,Test14,Test15,Test16,Test17,Test18,Test19,Test20,Test21,Test22,Test23,Test24";

$output .= " \n";
    
$fp = fopen( $OutFile, "a" );
$fout = fwrite( $fp, $output );
fclose( $fp );

echo "<a href=\"../" . $OutFile . "\" target=\"_blank\">" . $destination_file . "</a><br>\n\n";

chmod( $OutFile, 0777 );
?>

Still getting errors when used with Facebook.. what confuse me is if I open the file with Excel "Do nothing" just save it and upload it again, the file is accepted by Facebook Feed.

Could be something related to the mode parameter "a" in fopen( $OutFile, "a" ); ?

Edited by ludwig
Link to comment
Share on other sites

Also I tried this code :

define('FEEDNAME', 'catalog_products.csv');

$OutFile = "feeds/" . FEEDNAME;
$destination_file = FEEDNAME;
$source_file = $OutFile;

if ( file_exists( $OutFile ) ) {
   unlink( $OutFile );
}

$list = array (
array("id","title","description","availability","condition","price","link","image_link","brand","google_product_category","fb_product_category","quantity_to_sell_on_facebook","sale_price","sale_price_effective_date","item_group_id","gender","color","size","age_group","material","pattern","shipping","shipping_weight","style"),
    
array("Test1","Test2","Test3","Test4","Test5","Test6","Test7","Test8","Test9","Test10","Test11","Test12","Test13","Test14","Test15","Test16","Test17","Test18","Test19","Test20","Test21","Test22","Test23","Test24")
);

$file = fopen($OutFile,"w");

foreach ($list as $line) {
  fputcsv($file, $line);
}
fclose($file);

Same errors, and also if I save the file with Excel, the magic happen, Facebook like it 😕

Link to comment
Share on other sites

4 minutes ago, Barand said:

Have you tried comparing your file with one you get from Excel to see if there are differences that would account for its acceptance?

Yes, looks the same, also I opened both on notepad to compare.

I will attached both files, before and after:

https://www.watercenter.me/feeds/catalog_products_before.csv (Not working)

https://www.watercenter.me/feeds/catalog_products_after.csv (Working)

Cheers!

Link to comment
Share on other sites

  • Solution

These are the differences I can see

  1. Your "after" file has a comma at the end of line 1, the "before" one doesn't.
    image.thumb.png.f7496e8c31a1314ddb1344454c67a502.png
     
  2. Your "after" file has a newline character at the end of the final line (extra blank line at end of file); "before" doesn't.
    image.thumb.png.f54db488e90e9b1edb82f11486bb86ca.png
     
  3. "After" prices not rounded to 2 dec; "before" ones are. (123.50 becomes 123.5 etc)
    image.thumb.png.f78f6c4ae6da120996394eb5912348c0.png
Link to comment
Share on other sites

30 minutes ago, Barand said:

These are the differences I can see

  1. Your "after" file has a comma at the end of line 1, the "before" one doesn't.
    image.thumb.png.f7496e8c31a1314ddb1344454c67a502.png
     
  2. Your "after" file has a newline character at the end of the final line (extra blank line at end of file); "before" doesn't.
    image.thumb.png.f54db488e90e9b1edb82f11486bb86ca.png
     
  3. "After" prices not rounded to 2 dec; "before" ones are. (123.50 becomes 123.5 etc)
    image.thumb.png.f78f6c4ae6da120996394eb5912348c0.png

Many thanks for this visibility, what software are you using to compare ?

Link to comment
Share on other sites

I added a comma at the end of header line and a break line at the end of file, my code is similar to this now:

<?php

define( 'FEEDNAME', 'catalog_products.csv' ); 

$OutFile = "feeds/" . FEEDNAME;
$destination_file = FEEDNAME;
$source_file = $OutFile;

if ( file_exists( $OutFile ) ) {
    unlink( $OutFile );
}

$output = "id,title,description,availability,condition,price,link,image_link,brand,google_product_category,fb_product_category,quantity_to_sell_on_facebook,sale_price,sale_price_effective_date,item_group_id,gender,color,size,age_group,material,pattern,shipping,shipping_weight,style,";

$output .= "\n";

$output .= "Test1,Test2,Test3,Test4,Test5,Test6,Test7,Test8,Test9,Test10,Test11,Test12,Test13,Test14,Test15,Test16,Test17,Test18,Test19,Test20,Test21,Test22,Test23,Test24";
$output .= " \n";
$output .= " \n";
    
$fp = fopen( $OutFile, "a" );
$fout = fwrite( $fp, $output );
fclose( $fp );

echo "<a href=\"../" . $OutFile . "\" target=\"_blank\">" . $destination_file . "</a><br>\n\n";

chmod( $OutFile, 0777 );
?>

Thanks @Barand Facebook now initiated the process without errors

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.