Reputation: 115
Should I be able to use PHP Curl to upload a file to Sharepoint. I have this logic that I am using at the moment to "try" and upload to "My Shared Document" for now.
$the_file = file_get_contents('index.html');
$user = '&&&&&&&';
$pass = '&&&&&&&';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/20100101 Firefox/6.0Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, 'https://&&&&.net/personal/&&&&&&&/Shared%20Documents/Forms/AllItems.aspx');
curl_setopt($ch, CURLOPT_POST, true);
$post = array('file_contents' => "the_file");
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
if(curl_errno($ch)) {
echo json_encode('Error: ' . curl_error($ch));
}
else {
echo json_encode($response);
}
Any help is much appreciated.
Thanks
Martin
Upvotes: 0
Views: 3705
Reputation: 39355
When you upload a file through curl, few things you need to consider.
use @ with file path to upload the file directly. example:
$post = array('file_contents' => "@/home/xyz/the_file");
// if you have parameter with the file, then it will be,
$post = array('uid' => 110, 'file_contents' => "@/home/xyz/the_file");
2. You don't need to specify this header "Content-type: multipart/form-data". It will be added automatically when you use @ with your file parameter.
Upvotes: 1