I'm trying to copy images from an array of URLs with the following code. The problem is it only saves the last picture, anyone knows why is this?
foreach($photos['data'] as $photo)
{
echo "guardando:";
echo $photo['id'];
$idFoto++;
$fp = fopen('foto'.$idFoto.'.jpg', 'wb');
curl_setopt($ch, CURLOPT_URL, $photo['source']);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
I think your problem is in curl_close($ch), this call should be outside for bucle or maybe you could call curl_init for each photo also could check if curl_setopt is returning true or false:
foreach($photos['data'] as $photo)
{
echo "Guardando $idFoto: $photo['source']\n";
$idFoto++;
$fp = fopen('foto'.$idFoto.'.jpg', 'wb');
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $photo['source']);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
if(curl_exec($ch)===false) echo "Error: ".curl_error()."\n";
fclose($fp);
curl_close($ch);
}
Also note CURLOPT_RETURNTRANSFER is not needed because we are saving to file and not assigning curl_exec return. Could you test please?
Related
this code was working for a period of time pretty good:
<?php
$ch = curl_init("***");
$fp = fopen("example_homepage.html", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
but now it's giving me page with :" Object Moved This document may be found here"
i tried to solve it with "curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);" :
<?php
$ch = curl_init("***");
$fp = fopen("example_homepage.html", "w");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
but now it's giving me blank page, I'm doing something wrong? I tried some other things but it was always giving me blank one. I don't know much about programming, hope that you guys can help me
If you are not going to send any sensitive data, a quick fix would be to turn off the SSL verification,
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
I want to save the below link's output to file.xml but it is not working for me. It just displays the output on another browser.
$url = 'http://www.forexwire.com/feed/full?username=alumfx&password=T7M9Exb4';
$fp = fopen (dirname(__FILE__). '/file.xml', 'w+');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
$ch->save('file.xml');
fclose($fp);
By default CURL's exec function returns the result as standard output. You need to add this to make it return the result as a string:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
then just save it into a variable
$output = curl_exec($ch);
//do whatever with the $output
So that the complete code snippet can look like this:
$ch = curl_init('http://www.forexwire.com/feed/full?username=alumfx&password=T7M9Exb4');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
$output = curl_exec($ch);
curl_close($ch);
file_put_contents('path/to/file', $output);
I have function to download remote file based on offset and limit.
function get_part_file($url, $offset, $limit){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RANGE, ''.$offset.'-'.$limit.'');
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
I wanna download 1024 bytes from end but this function download whole file. So, how can I do that?
If the remote host does not support Range headers, it doesn't matter what you do: you won't be able to download a specific range.
$ch = curl_init(trim($remoteFile));
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RANGE, '0-102399');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false );
$fp = fopen($filepath, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
fclose($fp);
curl_close($ch);
I want to download a image from the url but it is not getting downloaded.Please check out the code and tell me where i am going wrong.
<?php
$ch = curl_init ("http://l1.yimg.com/t/frontpage/cannes_anjelina_60.jpg");
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);
$fp = fopen("img.jpg",'w');
fwrite($fp, $rawdata);
fclose($fp);
?>
It's working fine, set proper file permissions to where the image should be saved. In this case it's the same folder where your script is, might want to move it somewhere else like:
// where "images" folder can be written with files
// set permissions to 0755
$fp = fopen("images/img.jpg",'w');
function download_image ($url, $the_filename) {
$cookie = tempnam ("/tmp", "CURLCOOKIE");
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Debian) Firefox/0.10.1");
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLINFO_EFFECTIVE_URL, true);
curl_setopt($ch, CURLOPT_REFERER, "http://tomakefast.com");
curl_setopt($ch, CURLOPT_POST, false);
$rawdata=curl_exec($ch);
curl_close($ch);
file_put_contents("../somewhere/". $the_filename, $rawdata);
}
If you see any problems, please let me know. This occasionally gives me a 0 byte image file but that could happen for any number of reasons. This could be improved to return false on 0 bytes, maybe even do a basic test to see if indeed an image was downloaded.
you can use this simple code instead if you feel this is better.
$img_file = file_get_contents("http://l1.yimg.com/t/frontpage/cannes_anjelina_60.jpg");
file_put_contents("myImage.jpg", $img_file);
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