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);
Related
I have a problem with my curl :
I connect to a page with CURL. then I redirect myself to a page that is available when you are connected. and then I would like to retrieve information on this page. But how can I do to get the html code? I have the page that is displayed, but I can't get the source code for scrap
$url = "https://www.mywebsite.com/porte_transferer.php";
curl_setopt ($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
return true with var dump
with that, i see the page, but idk how recover html for scrap my information
Thank for your help
$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
if(curl_error($ch)) {
fwrite($fp, curl_error($ch));
}
curl_close($ch);
fclose($fp);
source
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?
I have the following code for downloading images from remote URL's - the code seems to work ok except where there is a space in the URL - i've put in the str_replace to try to fix this but the problem persists - can anyone suggest what i'm doing wrong when they download they appear as 0kb files.
$image_url = str_replace(" ", "%20", $image_url);
$fp = fopen($source_image, "wb");
$ch = curl_init($image_url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
Ok sussed it - the site uses https on the images (god knows why) the following worked..
$fp = fopen($source_image, "wb");
$ch = curl_init($image_url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
I'm trying to do a CURL PUT with a file but I'm having issues.
Here is my code:
$url_path_str = 'http://my_url';
$file_path_str = '/my_file_path';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ''.$url_path_str.'');
curl_setopt($ch, CURLOPT_PUT, 1);
$fh_res = fopen($file_path_str, 'r');
$file_data_str = fread($fh_res, filesize($file_path_str));
curl_setopt($ch, CURLOPT_INFILE, $fh_res);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_path_str));
fclose($fh_res);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_response_res = curl_exec ($ch);
The script keeps timing out and I'm not sure why.
I'd appreciate some assistance. Thanks.
I figured this out. It happened to be fclose that was causing the issue. I simply put it after curl_exec.
Here's the amended code:
$url_path_str = 'http://my_url';
$file_path_str = '/my_file_path';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ''.$url_path_str.'');
curl_setopt($ch, CURLOPT_PUT, 1);
$fh_res = fopen($file_path_str, 'r');
curl_setopt($ch, CURLOPT_INFILE, $fh_res);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_path_str));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_response_res = curl_exec ($ch);
fclose($fh_res);
Hope this is helpful to someone else later.
Cheers.
I'm not 100% sure this is the problem, but the only one I can see is that you are reading the contents of the file with fread() when you don't need to, and are not doing anything with the data. This will leave the pointer at the end of the file, and cURL will be waiting for data from the pointer that it will never get.
Remove this line and see if your still have the problem:
$file_data_str = fread($fh_res, filesize($file_path_str));
Alternatively, if you do actually need the data from the file and are using the $file_data_str variable later in the script, place this line immediately after the one mentioned above:
rewind($fh_res);
EDIT
Also, I'm not sure about this at all: ''.$url_path_str.'' - you should just be able to do this $url_path_str and it will have the same effect but be (slightly) more resource-efficient.
If you are absolutely sure that the connection is being timed out and nothing else.. then you can add the following lines of code-
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1000);
curl_setopt($ch, CURLOPT_TIMEOUT, 10000 );
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