I have an url with an image: http://www.example.com/image.jpg
and I have an action in the controller that uploads a file to the server by user input. I want to use this same action to upload a file by url. I started to get the data from image:
$url = "http://www.example.com/image.jpg";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec($ch);
curl_close ($ch);
Now how do I send the $rawdata to the controller to be handled like a normal user upload?
Thanks
I figured it out myself.
$url = "http://www.example.com/image.jpg";
$filename = basename($url, rand_string(7));
$path = realpath("../webroot/uploads/")."/".$filename;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec($ch);
curl_close ($ch);
if(file_exists($path)){
unlink($path);
}
$file = fopen($path, 'x');
fwrite($file, $rawdata);
fclose($file);
$post_data['Filedata'] = '#'.$path;
$url = "http://localhost/controller/action";
$ch = curl_init();
// Set URL on which you want to post the Form and/or data
curl_setopt($ch, CURLOPT_URL, $url);
// Data+Files to be posted
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
// Pass TRUE or 1 if you want to wait for and catch the response against the request made
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// For Debug mode; shows up any error encountered during the operation
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// Execute the request
$response = curl_exec($ch);
echo $response;
In the controller:
function action(){
//get properties of the file
//$_FILES['Filedata']['name'];
//$_FILES['Filedata']['size'];
//$_FILES['Filedata']['tmp_name'];
//savefile();
}
If anyone knows how to post the file without saving it will be great.
Related
Login page - https://b2b.chiemsee.com/customer/account/login/
After successful login page - https://b2b.chiemsee.com/customer/account/loginPost/
Page I want to go to - https://b2b.chiemsee.com/damen
I have made 3 separate requests:
1) To get the CSRF token.
2) To make the login.
3) To navigate to the url I want to go to.
Until I added the third request, the login was working fine and taking me to loginPost/ page but now it takes me back to the login/ page. Does it have something to do with the cookie in the third request?
PHP:-
// login request one to get form_key
include('simple_html_dom.php');
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, "https://b2b.chiemsee.com/customer/account/login/");
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true); // to allow redirections
curl_setopt($ch2, CURLOPT_COOKIEJAR, 'cookie.txt'); // to save cookie data for login
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true); // to get the html
$response2 = curl_exec($ch2);
curl_close($ch2);
$html = new simple_html_dom();
$html->load($response2);
$val = $html->find('input[name=form_key]');
$form_key = $val[0]->value;
// login request two to send form_key, username and password
$data = array(
'form_key' => $form_key,
'login[username]' => 'user',
'login[password]' => 'pass',
'send' => 'Anmelden'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://b2b.chiemsee.com/customer/account/loginPost/");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // to allow redirections
curl_setopt($ch, CURLOPT_POST, true); // to send info
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); // to read data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // to get the html
$response = curl_exec($ch);
if (curl_error($ch)) {
$error_msg = curl_error($ch);
var_dump($error_msg);
exit;
}
curl_close($ch);
// request three to open first category
$ch3 = curl_init();
curl_setopt($ch3, CURLOPT_URL, "https://b2b.chiemsee.com/damen");
curl_setopt($ch3, CURLOPT_FOLLOWLOCATION, true); // to allow redirections
curl_setopt($ch3, CURLOPT_COOKIEFILE, 'cookie.txt'); // to read data
curl_setopt($ch3, CURLOPT_RETURNTRANSFER, true); // to get the html
$response3 = curl_exec($ch3);
if (curl_error($ch3)) {
$error_msg3 = curl_error($ch3);
var_dump($error_msg3);
exit;
}
curl_close($ch3);
echo $response3;
I figured out the problem.
I wasn't saving the new data (username and password) sent in second request, in the cookie file, with:
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); // to save cookie data for login
Updated code:
// login request one to get form_key
include('simple_html_dom.php');
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, "https://b2b.chiemsee.com/customer/account/login/");
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true); // to allow redirections
curl_setopt($ch2, CURLOPT_COOKIEJAR, 'cookie.txt'); // to save cookie data for login
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true); // to get the html
$response2 = curl_exec($ch2);
curl_close($ch2);
$html = new simple_html_dom();
$html->load($response2);
$val = $html->find('input[name=form_key]');
$form_key = $val[0]->value;
// login request two to send form_key, username and password
$data = array(
'form_key' => $form_key,
'login[username]' => 'user',
'login[password]' => 'pass',
'send' => 'Anmelden'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://b2b.chiemsee.com/customer/account/loginPost/");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // to allow redirections
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // to avoid error
curl_setopt($ch, CURLOPT_POST, true); // to send info
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); // to read data
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); // to save cookie data for login
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // to get the html
$response = curl_exec($ch);
if (curl_error($ch)) {
$error_msg = curl_error($ch);
var_dump($error_msg);
exit;
}
curl_close($ch);
//echo $response;
// request three to open first category
$ch3 = curl_init();
curl_setopt($ch3, CURLOPT_URL, "https://b2b.chiemsee.com/damen");
curl_setopt($ch3, CURLOPT_FOLLOWLOCATION, true); // to allow redirections
curl_setopt($ch3, CURLOPT_COOKIEFILE, 'cookie.txt'); // to read data
curl_setopt($ch3, CURLOPT_RETURNTRANSFER, true); // to get the html
$response3 = curl_exec($ch3);
if (curl_error($ch3)) {
$error_msg3 = curl_error($ch3);
var_dump($error_msg3);
exit;
}
curl_close($ch3);
echo $response3;
2) To make the login.
that code never worked in the first place, you just have insufficient login-error-checking code to realize it.
replace
$response = curl_exec($ch);
if (curl_error($ch)) {
$error_msg = curl_error($ch);
var_dump($error_msg);
exit;
}
curl_close($ch);
with
$response = curl_exec($ch);
if (curl_error($ch)) {
$error_msg = curl_error($ch);
var_dump($error_msg);
exit;
}
$domd=#DOMDocument::loadHTML($response);
$xp=new DOMXPath($domd);
$login_errors=array();
foreach($xp->query("//*[contains(#class,'error-msg')]") as $login_error){
$login_error=trim($login_error->textContent);
if(!empty($login_error)){
$login_errors[]=$login_error;
}
}
if(!empty($login_errors)){
throw new \RuntimeException("login errors: ".print_r($login_errors,true));
}
curl_close($ch);
and you'll see that you get an error during login.
$camera_ip = "10.10.10.10";
$image_url = "http://$camera_ip/cgi-bin/image.jpg?camera=right&size=1024x768&quality=60";
$ch = curl_init($image_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$rawdata = curl_exec($ch);
curl_close($ch);
$fp = fopen('./logo.jpg', 'wb');
fwrite($fp, $rawdata);
fclose($fp);
A file is being saved at 7KB but the size is 0x0 ... I can't figure out why the file isn't being saved correctly. My guess is that the file isn't being loaded fully before being saved, but is there a way of ensuring that?
Try using "w" instead of "wb" $fp = fopen('./logo.jpg', 'w');
Or try: file_put_contents('./logo.jpg',$rawdata); (not sure it is binary safe, should be)
And
Check your curl result to see what came back.
$info = var_export(curl_getinfo($ch),true);
file_put_contents('log.txt',$info);
OR
header('content-type: text/plain');
var_export(curl_getinfo($ch));
UPDATE
The curl Request failed (returned false).
Now you need to find out WHY.
For now change your:
curl_setopt($ch, CURLOPT_HEADER, 0);
To:
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
Add these options for trouble shooting:
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$data = curl_exec($ch);
if (curl_errno($ch)){
echo ' Retrieve Base Page Error: ' . curl_error($ch);
}
else {
$info = rawurldecode(var_export(curl_getinfo($ch),true));
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
$responseHeader= substr($data,0,$skip);
$data= substr($data,$skip);
echo "HEADER: $responseHeader\n";
echo "\n\nINFO: $info\n\nDATA: $data";
}
You will likely get the "Retrieve Base Page Error:" but it will also tell WHY it failed. If you cannot echo, replace echo with $info .=
I want to upload the image to the url using Curl.
But as the Image file is on https url i am not able to read the file using fopen.
Code is as below.
$file = "https://xyz.com/image.jpg";
$url = "http://abc.com/upload.php";
$fp = fopen($file, "r");
$headers = array("Content-Type: xml");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$responseInfo = curl_getinfo($ch);
curl_close($ch);
I think that this should work assuming that you don't need to pass the file data that you're uploading in as a named key/value pair.
$file = "https://xyz.com/image.jpg";
$url = "http://abc.com/upload.php";
$fileData = file_get_contents($file);
$fp = fopen($file, "r");
$headers = array("Content-Type: xml");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fileData);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$responseInfo = curl_getinfo($ch);
curl_close($ch);
Instead of:
curl_setopt($ch, CURLOPT_PUT, true);
opt setting for PUT API request will work fine on using this setting below:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
How do I download an external image off a url with cURL?
See here:
http://www.edmondscommerce.co.uk/php/php-save-images-using-curl/
function save_image($img,$fullpath){
$ch = curl_init ($img);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec($ch);
curl_close ($ch);
if(file_exists($fullpath)){
unlink($fullpath);
}
$fp = fopen($fullpath,'x');
fwrite($fp, $rawdata);
fclose($fp);
}
Other articles/sources:
http://forums.digitalpoint.com/showthread.php?t=371632
http://www.bitrepository.com/download-image.html
http://php.bigresource.com/Track/php-Jjg3DsKY/
from php.net - also reads into a string.
<?php
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "example.com");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
?>
I'm using the Amazon AIMS API to upload a an inventory file and I'm having an issue with the cURL call to upload the file. The documentation is very limited, so there is no example code that helps out in this.
This is what I have so far of the cURL call:
// $FILENAME is filename of the CSV file being uploaded:
$inventory = fopen($FILENAME, 'r') or die("Can't open file!");
echo $inventory;
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_INFILE, $inventory);
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filename));
curl_setopt($ch, CUROPT_PUT, TRUE);
$response = curl_exec($ch);
curl_close($ch);
I've try putting $inventory into the CURLOPT_POSTFIELDS and not having an INFILE, but I get the same error.
On the XML response, I'm getting "NO_FILE_ATTACHED" so the obvious issue is getting the file to be attached to the XML call.
I also tried uploading as the first responder said using the Example #2 on the curl_setopt page on php.net.
For that, I used the following code:
$data = array('#/tmp/amazon_export.csv');
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
curl_close($ch);
I got the same NO_FILE_ATTACHED response back.
Any ideas?
This works for me:
$hCurl = curl_init();
curl_setopt($hCurl, CURLOPT_PUT, true);
curl_setopt($hCurl, CURLOPT_HEADER, true);
curl_setopt($hCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($hCurl, CURLOPT_CONNECTTIMEOUT, CURL_TIMEOUT_SECS);
curl_setopt($hCurl, CURLOPT_URL, "$oMessage->url/att/$fid");
curl_setopt($hCurl, CURLOPT_HTTPHEADER, $aCurlHeaders);
// TODO it could be possible that fopen() would return an invalid handle or not work altogether. Should handle that
$fp = fopen ($finfo['tmp_name'], "r");
curl_setopt($hCurl, CURLOPT_INFILE, $fp);
curl_setopt($hCurl, CURLOPT_INFILESIZE, $finfo['size']);
$sResp = curl_exec($hCurl);
You are combining PUT and POST in a single curl operation, which will not work. Refer to example #2 of the curl_setopt manual page for an example on how to upload a file using POST.
you have $filename and $FILENAME, the filesize call should be in your case filesize($FILENAME)...
Hope thats helps