Upload a local file to a Facebook page using curl PHP - php

Actually I'm able to upload a file from a http url to my Facebook page with this code:
$data = array();
$data['url'] = "http://www.example.com/image.jpg";
$data['access_token'] = $fb_page_access_token;
$post_url = 'https://graph.facebook.com/'.$fb_graph_version.'/'.$fb_page_id.'/photos';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$return = curl_exec($ch);
Now I need to change this code, sending a photo from local memory of pc. For example a file called "test.jpg" inside the same dir of the script, but I don't find any example on facebook doc about the name to use. https://developers.facebook.com/docs/graph-api/photo-uploads/
Any suggestions how to change my curl request for local files?

Related

How to add product images with Prestashop API

I'm trying to add/upload product images via Prestashop API, but I'm getting server error 500.
What can be wrong with the code? Or maybe there's something wrong with the server configuration?
PHP script:
error_reporting(-1);
ini_set('display_errors', 'On');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://MY_AUTH_KEY#my-shop.com//api/images/products/24/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, 'MY_AUTH_KEY:');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' =>'#http://my-shop.com/img/my-shop-logo-1584646645.jpg;type=image/jpg'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$curlinfo = curl_getinfo($ch);
curl_close($ch);
print_r($curlinfo);
This results in [http_code] => 500. There's no error or anything. I have access to the hosting provider's server error log, but there's nothing in there...
The script is based on the Prestashop docs: https://devdocs.prestashop.com/1.7/development/webservice/tutorials/change_product_image/
Try with :
error_reporting(-1);
ini_set('display_errors', 'On');
$image_path = 'http://my-shop.com/img/my-shop-logo-1584646645.jpg';
$image_mime = 'image/jpg';
$args['image'] = new CurlFile($image_path, $image_mime);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_URL, 'http://my-shop.com/api/images/products/24/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, 'MY_AUTH_KEY:');
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$curlinfo = curl_getinfo($ch);
curl_close($ch);
print_r($curlinfo);
I've found the answer - You cannot upload remote files with cURL. It can only send local files. So you need to download the file, save it locally and then use cURL with local path.
Source:
How can I use cURL's '#' syntax with a remote URL?

Download Google Docs Spreadsheet via PHP

I'm trying to download and parse a google spreadsheet directly via PHP.
Do i need to download the exported CSV file from google docs a different way?
$csv = file_get_contents('https://docs.google.com/spreadsheets/d/KEY/export?format=csv');
print_r($csv);
I tried it with file too:
$csv = file('https://docs.google.com/spreadsheets/d/KEY/export?format=csv');
And with cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://docs.google.com/spreadsheets/d/KEY/export?format=csv");
curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$csv = curl_exec($ch);
curl_close($ch);
var_dump($csv);
When i call the URL from a browser i get a nice tiny CSV file. This way i get the source of the google docs website.

How to transfer GET data from one server to another using CURL?

I have two Servers:
1. Licensing
2. Provisioning
Now a client(android/ios device) sends a request to license server. So there we get some parameters from GET which I want to forward to Provisioning server, using CURL.
This code is on License Server
$skey = $this->input->get('site_id');
$uid = $this->input->get('user_id');
$url = "http://127.0.0.1/provisioning/customer/provisioning?site_key=".$skey."&uid=".$uid;
$wget_cmd = "wget --no-check-certificate \"".$url."\" >/dev/null 2>/dev/null ";
//echo($wget_cmd);
//exec($wget_cmd);
// create a new cURL resource
$ch = curl_init();
//curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
//curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
echo curl_error($ch);
// close cURL resource, and free up system resources
curl_close($ch);
On the Provisioning Server How to access these variables?:
$skey = $this->input->get('site_id');
$uid = $this->input->get('user_id');
I don't want to echo them on screen I Just want to use them for other DB transactions.

Get currency exchange rate from bank site

I'm trying to get content from bank site using curl.
http://www.zaba.hr/home/wps/wcm/connect/zaba_hr/zabapublic/tecajna
Site is specific becouse it using ajax to fill currency exchange table. There is a link for download data in to file but you have to have same session id to able to do that.
Im trying this code:
$url="http://www.zaba.hr/home/wps/wcm/connect/zaba_hr/zabapublic/tecajna";
$useragent = $_SERVER['HTTP_USER_AGENT'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_URL,$url);
$cl = curl_exec($ch);
$dom = new DOMDocument();
#$dom->loadHTML($cl);
#$link = $dom->getElementById('tecajPrn');
echo $suburl = "http://www.zaba.hr".$link->getAttribute('href');
After this I got link to file but I can't open it.
Another strange situation is that link I got with curl is http://www.zaba.hr/home/ZabaUtilsWeb/utils/tecaj/danasPrn but real link when I click on icon is http://www.zaba.hr/ZabaUtilsWeb/utils/tecaj/prn/62/2014
You are messing with cookie and ajax(may be!). Here is the lookaround. Try this:
First send a request to the page to obtain the cookie.
$url="http://www.zaba.hr/home/wps/wcm/connect/zaba_hr/zabapublic/tecajna";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "mozilla 5.0");
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_COOKIEFILE,"cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR,"cookie.txt");
$cl = curl_exec($ch);
curl_close($ch);
After that make another curl request. This time to obtain the json data:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "mozilla 5.0");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Requested-With: XMLHttpRequest", "Referer: http://www.zaba.hr/home/wps/wcm/connect/zaba_hr/zabapublic/tecajna"));
curl_setopt($ch, CURLOPT_URL,"http://www.zaba.hr/ZabaUtilsWeb/utils/tecaj/danas");
curl_setopt($ch, CURLOPT_COOKIEFILE,"cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR,"cookie.txt");
$cl = curl_exec($ch);
curl_close($ch);
Your json is available at this variable. Parse it using json_decode()
// now parse json from $cl
print $cl;
Anything required, help yourself straightway!
Note: Make sure you have write permission for the cookie.txt file. Also, its better to use absolute path like c:/test/cookie.txt or /var/tmp/cookie.txt.

CURL Post looping

I am running a script which uploads a file to remote form
//submit form
$form_data = array("file_upload" => "#file.xml;type=text/xml","otherkey" => "overvalue");
$ch = curl_init();
$form_url = 'http://www.domain.com/form.php';
curl_setopt($ch, CURLOPT_URL, $form_url);
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $form_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
$postResult = curl_exec($ch);
echo $postResult;
The file is being uploaded perfectly, however on the remote server, after file upload and processing the page is reloaded (form.php), however the CURL post simply begins again.
I am guessing this is due to the POST data still being present? how can I do this so that the file is uploaded once, processed and then completed? I have set FOLLOWLOCATION = 0 but with no luck
Many thanks,
How do you know it begins again? If you infer it from your echo, it is echoed twice as you write this:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); //echoes result at init
instead of this
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //returns only result, not echoing

Categories