This is my curl code:
$fp = fopen('path/to/file/'. $id . '.pdf', 'w+');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'myurl');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, '');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_COOKIEJAR, '');
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_FILE, $fp);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
curl_exec($curl);
I managed to download my file. I can open it , but not in Chrome. Plus, if I am trying to open it with $pdf = new Zend_Pdf(file_get_contents($pdfFile)); I am receiving the following error: File is not a PDF
I believe the way how I am saving under a specific name and put it on a specific folder, is not correct. Any way, ideas how to approach this ?
Thank you
When you are trying to retrieve "structured" files such as a PDF you do not want the headers returned too as they will usually corrupt the file being retrieved. Change
curl_setopt($curl, CURLOPT_HEADER, 1);
to
curl_setopt($curl, CURLOPT_HEADER, 0);
Related
Is there any way to get file contents of external webiste, say https://www.example.com/.
For eg. Using Python from shell_exec. I tried python but got blank.
Not sure why you want to use shell_exec but
you can use
$data = file_get_contents('https://www.example.com/');
or
$url = 'https://www.example.com/';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);
if you want to maintain the session you could use curl with CURLOPT_COOKIEFILE
eg:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile");
curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_URL, 'https://www.example.com/');
$data = curl_exec($ch);
or if you really want to use the shell_exec the use follwing
$data = shell_exec('curl -I https://www.example.com/');
echo "<pre>$data</pre>";
however for above to work you have to have curl installed check following for instillation of curl
https://curl.se/
hope this works
When I try to save cookies without Laravel,it's saved.But with Laravel I can't find the cookie file(Searched everywhere :) ).
Here is the code(part):
...Some php code...
$dataJSON = json_encode($dataPost);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $dataJSON);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_COOKIEJAR, "cookie.txt"); //even with the realpath ,no difference
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($curl);
print_r($result);
curl_close($curl);
Is it a Routing problem?
I have to get filesize of a remote file by using curl in PHP
the file is hosted on Dropbox which will be downloaded by direct link
below is my code what I have written
$szUrl='https://www.dl.dropboxusercontent.com/s/8ol4o753smure2o/Holy_Quran_in_Turkish.pdf';
$curl = curl_init();
curl_exec($curl);
curl_setopt($curl, CURLOPT_URL, $szUrl);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_NOBODY, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$size = curl_getinfo($curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
echo $size;
curl_close($curl);
The output is -1 with the code, How to get the length of the iso file properly with php_curl?
You have to set the curl options before you exec:
<?php
$szUrl='https://www.dl.dropboxusercontent.com/s/8ol4o753smure2o/Holy_Quran_in_Turkish.pdf';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $szUrl);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_NOBODY, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_exec($curl);
$size = curl_getinfo($curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
echo $size;
curl_close($curl);
I need some help, I have a ftp access and my problem is I want to display or get the content of a specific file since I will use as a variable in php. How is it possible? I have this code but it just show the content of the directory.
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "ftp://$HOST");
curl_setopt($curl, CURLOPT_USERPWD, "$USER:$PASS");
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1) ;
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'CWD /$PATH');
curl_exec($curl);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'MLSD');
$ftp_result=curl_exec($curl);
echo $ftp_result;
Try
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "ftp://$HOST/$PATH");
curl_setopt($curl, CURLOPT_USERPWD, "$USER:$PASS");
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
$ftp_result=curl_exec($curl);
echo $ftp_result;
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);