How to achieve an image upload via cURL?
I was told to use curl_file_create but it's not working.
This is the array from post when I manually (using Chrome) upload a file and see the raw structure:
Array
(
[image] => Array
(
[0] => 377529090726537.jpg
)
[thumbnail_digest] => Array
(
[0] => EMPTYDIGEST
)
[digest_present] => Array
(
[0] => 0
)
[image_rotation] => Array
(
[0] => 0
)
)
Inside my cURL I tried:
$cfile = curl_file_create('IMG_33071.jpg','image/jpeg','test_name');
$post_data = array(
'image' => $cfile,
);
But after running the script, I receive this error:
Catchable fatal error: Object of class CURLFile could not be converted to string in C:\\Path\\to\\my\\server\\curl.php on line 144
The image is on the same folder from curl.php file. My php version is 5.5.
I also tried this:
$cfile = curl_file_create('http://www.siteexample.com/path/to/my/file.jpg','image/jpeg','test_name');
But no success :(
My structure:
//These are the post data username and password
$login_data = 'email='.$email.'&password='.$password.'&entrar=Entrar';
//Create a curl object
$login = curl_init();
//Set the useragent
$agent = $_SERVER["HTTP_USER_AGENT"];
//$agent = 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0';
curl_setopt($login, CURLOPT_USERAGENT, $agent);
curl_setopt($login, CURLOPT_SSL_VERIFYPEER, false);
//Set the URL
curl_setopt($login, CURLOPT_URL, $login_url );
//This is a POST query
curl_setopt($login, CURLOPT_POST, 1 );
//Set the post data
curl_setopt($login, CURLOPT_POSTFIELDS, $login_data);
//We want the content after the query
curl_setopt($login, CURLOPT_RETURNTRANSFER, 1);
//Follow Location redirects
curl_setopt($login, CURLOPT_FOLLOWLOCATION, 1);
/*
Set the cookie storing files
Cookie files are necessary since we are logging and session data needs to be saved
*/
curl_setopt($login, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($login, CURLOPT_COOKIEFILE, 'cookie.txt');
//Execute the action to login
$LoginResult = curl_exec($login);
echo $LoginResult;
//create array of data to be posted
$post_data = array(
'name' => 'Name',
'email' => $email,
'email_confirm' => $email,
'phone' => '9999999999',
'passwd' => $password,
'passwd_ver' => $password,
'create' => '',
);
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);
var_dump($post_string);
curl_setopt($login, CURLOPT_URL, 'http://urltocurl.com/verify');
curl_setopt($login, CURLOPT_POST, false);
curl_setopt($login, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($login, CURLOPT_TIMEOUT, 0);
$answer = curl_exec($login);
echo $answer;
if (curl_error($login)) {
echo curl_error($login);
}
//close the connection
curl_close($login);
Now take a look at the array at the beggining.
Array
(
[image] => Array
(
[0] => 377529090726537.jpg
)
It is an array inside another one. How to achieve that? Maybe curl_file_create is working but it's sending the image to a wrong location of array.
Look at my example it should work for you:
$ch = curl_init();
$data = array('name' => 'Foo', 'file' => '@/path/to/image.jpeg');
curl_setopt($ch, CURLOPT_URL, 'http://example.com/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.