PHP Curl - Upload file to Sharepoint - php

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

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.

Related

php get error to curl url from array get in file

I get Error When Curl Data From URL ( Url From Array get in file ) .
When I use Normaly URL , It Work Correctly !
But When I get URL from Array and run a loop for it , and call function curl to run every line url , it not work ?
I don't know why , but i need someone help me .
I had turn on error report php , but nothing error !
Example File "link.txt"
https://www.fshare.vn/file/4UNG2NRPW5OQ
https://www.fshare.vn/file/CFTJYXKPMN7Q
https://www.fshare.vn/file/RMHD2XBFY93F
https://www.fshare.vn/file/I4TIUE6E9QOV
https://www.fshare.vn/file/5PV15EB38M7T
https://www.fshare.vn/file/ZDLJNDNWCNYM
https://www.fshare.vn/file/O1VYZUXXJNCI
https://www.fshare.vn/file/SD5C1VZ38IPN
And My PHP code
<?php
error_reporting(E_ALL);
$lines = file('link.txt', FILE_IGNORE_NEW_LINES); // Turn Every Line To Array From File , Or use other below Method
//$lines = explode("\n",fopen('link.txt',"r")); // Other Method Get Content To Array
//$lines = explode("\n",file_get_contents('link.txt')); // Other Method Get Content To Array
foreach($lines as $key => $value){
echo $value . "</br>";
echo get_data($value); // It not Work , Why is that ?
echo get_data("https://www.fshare.vn/file/ACKM6ONXFZJE"); // It Work Normally
die();
}
function get_data($url = null) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type' => 'application/x-www-form-urlencoded', 'charset' => 'utf-8'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0");
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com.vn/');
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
Right now , i have tested again and it work correctly !
Don't know what happened !

Access Denied for CURL Post

I'm trying to login to Walmart using CURL, the code works perfectly. However whenever i activate the CURLOPT_POST I'm getting an error Access Denied.
This is the code that I tried:
<?php
$user_agent = "Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20140319 Firefox/24.0 Iceweasel/24.4.0";
$curl_crack = curl_init();
CURL_SETOPT($curl_crack,CURLOPT_URL,"https://www.walmart.com/account/login");
CURL_SETOPT($curl_crack,CURLOPT_USERAGENT,$user_agent);
//CURL_SETOPT($curl_crack,CURLOPT_PROXY,"183.78.169.60:37899");
//CURL_SETOPT($curl_crack,CURLOPT_PROXYTYPE,CURLPROXY_SOCKS5);
CURL_SETOPT($curl_crack,CURLOPT_POST,True);
CURL_SETOPT($curl_crack,CURLOPT_POSTFIELDS,"login-username=test&login-password=test");
CURL_SETOPT($curl_crack,CURLOPT_RETURNTRANSFER,True);
CURL_SETOPT($curl_crack,CURLOPT_FOLLOWLOCATION,True);
CURL_SETOPT($curl_crack,CURLOPT_COOKIEFILE,"cookie.txt"); //Put the full path of the cookie file if you want it to write on it
CURL_SETOPT($curl_crack,CURLOPT_COOKIEJAR,"cookie.txt"); //Put the full path of the cookie file if you want it to write on it
CURL_SETOPT($curl_crack,CURLOPT_TIMEOUT,30);
echo $exec = curl_exec($curl_crack);
?>
I don't know what I'm doing wrong, please help me.
Here is a working example.
You need to first request the home page to establish base cookies, and then post the credentials to the login page. If a 403 error is returned, the credentials are incorrect.
<?php
$base_url = 'https://www.walmart.com/';
$login_url = 'https://www.walmart.com/account/api/signin';
$user_agent = "Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20140319 Firefox/24.0 Iceweasel/24.4.0";
$username = 'user#example.com';
$password = 'password';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $base_url);
curl_setopt($ch, CURLOPT_USERAGENT,$user_agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,True);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
curl_setopt($ch, CURLOPT_COOKIEFILE, '');
curl_setopt($ch, CURLOPT_TIMEOUT,30);
//curl_setopt($ch, CURLOPT_VERBOSE, 1);
//curl_setopt($ch, CURLOPT_STDERR, fopen('php://output', 'r'));
//curl_setopt($ch,CURLOPT_PROXY,"183.78.169.60:37899");
//curl_setopt($ch,CURLOPT_PROXYTYPE,CURLPROXY_SOCKS5);
$resp = curl_exec($ch);
$headers = array(
'X-Requested-With: XMLHttpRequest',
);
$post = array(
'username' => $username,
'password' => $password,
'login-captcha-value' => '',
'sensor-data' => '',
'clearPCID' => '1',
);
curl_setopt($ch, CURLOPT_URL, $login_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$exec = curl_exec($ch);
$info = curl_getinfo($ch);
if ($info['http_code'] != 200) {
echo "Login failed! HTTP code {$info['http_code']}<br>\n";
var_dump($exec);
exit;
}
echo "Login successful!<br>\n";
// you are now logged in, use $ch to request pages as the logged in user
$url = 'https://www.walmart.com/account';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 0);
$account = curl_exec($ch);

Error in image upload

I want to post some data including a image.I am using curl in php for this.My code is below-
$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_USERAGENT, "Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/20100101 Firefox/6.0Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, 'http://postacity.co.uk:8080/shine/pp/upload/');
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
'creatorid' => '119',
'userfile' => '#/temp/fgf.jpg',
'notice' => 'Image1',
'catid' => '1',
'title' => 'bookofzeus',
'boardid' => '332',
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
if(curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
}
else {
echo $response;
}
}
Every time i am getting the same error
Error: failed creating formpost data
Am i doing any mistake.I have searched for this problem but still no solution found.
The path to the file is the problem: '#/temp/fgf.jpg'
You should take a look at this bug gescription: Bug 50060 - failed creating formpost data if post array value starts with #
The solutions of this problem for example at php freaks.
And use absolute path to the files.
Refer this its working
<?php
$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_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, _VIRUS_SCAN_URL);
curl_setopt($ch, CURLOPT_POST, true);
// same as <input type="file" name="file_box">
$post = array(
"file_box"=>"#/path/to/myfile.jpg",
"username"=>"foobar",
"password"=>"secret",
"submit"=>"submit"
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
?>
Also refer this url's
http://joshhighland.com/blog/2010/11/27/using-curl-and-php-to-upload-files-through-a-form-post/
http://blogs.digitss.com/php/curl-php/posting-or-uploading-files-using-curl-with-php/

PHP CURL seems to be ignoring cookie jar/file

I'm using this....
function cURL($url, $header=NULL, $p=NULL)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/TestCookies");
if ($p) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $p);
}
$result = curl_exec($ch);
if ($result) {
return $result;
} else {
return curl_error($ch);
}
curl_close($ch);
}
And I'm making a call like this...
$Headers = array(
"Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*\/*;q=0.5",
"Accept-Language: en-gb,en;q=0.5",
"Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7");
$a = $this->cURL("https://www.mysite.com/",$Headers, null);
I'd expect this to receive a cookie and write it to the file /tmp/TestCookies.
The site definitely returns Set-Cookie headers - I can see them if I dump $a, however, the file in question is never created.
In case it's a permissions issue, I created it with touch and chmod 777'd it - The file now exists but it's empty.
What am I doing wrong?
The curl constants are split into
CURL_COOKIEFILE as the cookies.txt source which is read from
and CURL_COOKIEJAR as the datastore that is written back to
You do have to provide _COOKIEFILE with an empty string to enable it IIRC, but _COOKIEJAR if you want them stored. Normally set both options to the same name.

Posting images via CURL to a form

I've had a tough time trying to find a solution for the following problem, I need to use curl to submit a form on a website, however it also needs to upload a picture its a normal input file field
<input type="file" name="image"/>
I have a class and the curl function is defined like this
function fetch($url, $username='', $data='', $proxy=''){
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
if(isset($proxy)) {
curl_setopt($ch,CURLOPT_TIMEOUT,30);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'proxyadmin:parola');
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT,true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/3.0 (compatible; MSIE 6.0; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
if($username) {
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie/{$username}.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie/{$username}.txt");
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
if (is_array($data) && count($data)>0){
curl_setopt($ch, CURLOPT_POST, true);
$params = http_build_query($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
}
if (is_array($this->headers) && count($this->headers)>0){
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
}
$this->result = curl_exec($ch);
$curl_info = curl_getinfo($ch);
$header_size = $curl_info["header_size"];
$this->headers = substr($this->result, 0, $header_size);
$this->http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$this->error = curl_error($ch);
curl_close($ch);
}
Anyone could help me? very lost
If you change the following block of code:
// from
if (is_array($data) && count($data)>0){
curl_setopt($ch, CURLOPT_POST, true);
$params = http_build_query($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
}
// to
if (is_array($data) && count($data)>0) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
And to have it post the file uploaded by the user, set up your data array like this:
// make sure $_FILES['file'] has been uploaded and is valid
$data = array('field' => 'value',
'name' => 'test',
'email' => 'something',
'file' => '#' . $_FILES['file']['tmp_name']
);
fetch($url, $username, $data);
That will tell curl to send a form post with a file upload. By setting the post fields to an array and prepending an & to the full path of a file that is a value of the array, curl will send an multipart/form-data post request with your file upload.
See curl file upload example

Categories