I'm attempting to download a file to server with curl. I'm having a problem with the filename and saving. The filename is in persian but when i check the file in filezilla it is encoded incorrectly. It should still show the persian character.
For example the file name is:
حواشی ثبت نام دوازدهمین دوره ریاست جمهوری/ خنده دار - قسمت هفتم (7)
But when i check filezilla the filename is:
â«ÙÛÙ٠سÛÙÙاÛÛ ÙردÙا Ùرشت٠Ù.mp4
curl.php
$destination = "/home/mywebsite/public_html/wp-content/my_channels/videos/".$videoTitle.".mp4";
$file = fopen($destination, 'w');
// cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$link);
// set cURL options
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING ,"");
// set file handler option
curl_setopt($ch, CURLOPT_FILE, $file);
// execute cURL
$result = curl_exec($ch);
$error = curl_error($ch);
// close cURL
curl_close($ch);
// close file
fclose($file);
How do i get the filename to have the persian characters?
Related
I want to get zip file from using PHP CURL request but when i hit that URL in browser it print like that.
I am using https://gist.github.com/thagxt/d9b4388156aeb7f1d66b108d728470d2 this as a reference.
Like the same i have created extracted folder var/www/html/extracted and my file apth is /var/www/html/data.php but it doesn't create zip file.
I have applied the proper permisssion on that folder as well but didn't any success.
I want to get zip file in that particular folder.
My code is
<?php
$url = "https://wordpress.org/latest.zip"; // URL of what you wan to download
$zipFile = "wordpress.zip"; // Rename .zip file
$extractDir = "extracted"; // Name of the directory where files are extracted
$zipResource = fopen($zipFile, "w");
// Get The Zip File From Server
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FILE, $zipResource);
$page = curl_exec($ch);
if(!$page) {
echo "Error :- ".curl_error($ch);
}
curl_close($ch);
/* Open the Zip file */
$zip = new ZipArchive;
$extractPath = $extractDir;
if($zip->open($zipFile) != "true"){
echo "Error :- Unable to open the Zip File";
}
/* Extract Zip File */
$zip->extractTo($extractPath);
$zip->close();
die('Your file was downloaded and extracted, go check.');
?>
Any help is appreciated.
try to put the response in the file_put_content function like this
file_put_contents('zipname.zip', $response);
I need to download a zipped .csv file from this website. http://www.phrfsocal.org/web-lookup-2/ The file is the link Download Data above the table on the right.
The gotcha is the link is created dynamically. So I need to extract it first.
That part seems to work fine. I get this link for the href.
https://b6.caspio.com/dp.asp?appSession=68982476236455965042483715808486764445346819370685922723164994812296661481433499615115137717633929851735433386281180144919150987&RecordID=&PageID=2&PrevPageID=&cpipage=&download=1
When I paste that link into a new browser tab, the browser downloads the zip file containing the csv that I am interested in.
However when a use CURL to try to get the zip, it instead gets the html of the table below the link. Can't seem to figure out how to grab the .zip.
Below is my code the first part finds the link and seems to be working.
The second part is where I having trouble.
PS I have permission from the owner of this page to download this data nightly using a Cron job.
thanks in advance,
Dave
$url = "http://www.phrfsocal.org/web-lookup-2/";
// url to the dynamic content doesn't seem to change.
$url = "https://b6.caspio.com/dp.asp?AppKey=0dc330000cbc1d03fd244fea82b4";
$header = get_web_page($url);
// Find the location of the Download Data link and extract the href
$strpos = strpos($header['content'], 'Download Data');
$link = substr($header['content'], $strpos, 300);
$link = explode(" ", $link);
$link = explode('"', $link[2]);
$url1 = $link[1];
print_r($url1);
print "<p>";
// Now Go get the zip file.
$zipFile = "temp/SoCalzipfile.zip"; // Local Zip File Path
$zipResource = fopen($zipFile, "w+");
// Get The Zip File From Server
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url1);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FILE, $zipResource);
$page = curl_exec($ch);
if (!$page) {
echo "Error :- " . curl_error($ch);
}
curl_close($ch);
echo "zip file recieved";
/* Open the Zip file */
$zip = new ZipArchive;
$extractPath = "temp";
if ($zip->open($zipFile) != "true") {
echo "Error :- Unable to open the Zip File";
}emphasized text
/* Extract Zip File */
$zip->extractTo($extractPath);
$zip->close();
The following code will download the zip file and unzip it into the given folder. Make sure that the folder is writable. So in this example make sure the temp folder has write permission.
You also don't need to fetch the html version of the page to extract the link. I had a play around with the URLs and you can get the zip file for each page by using the cpipage variable. You can change the $page_num variable to grab the zip from the given page.
$page_num = 1;
$url = 'https://b6.caspio.com/dp.asp?AppKey=0dc330000cbc1d03fd244fea82b4&RecordID=&PageID=2&PrevPageID=&cpipage=' .$page_num. '&download=1';
$zipFile = "temp/SoCalzipfile.zip"; // Local Zip File Path
$zipResource = fopen($zipFile, "w");
// Get The Zip File From Server
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FILE, $zipResource);
$page = curl_exec($ch);
if(!$page) {
echo "Error :- ".curl_error($ch);
}
curl_close($ch);
$zip = new ZipArchive;
$extractPath = "temp";
if($zip->open($zipFile) != "true"){
echo "Error :- Unable to open the Zip File";
}
/* Extract Zip File */
$zip->extractTo($extractPath);
$zip->close();
I was trying to upload image using curl put request, And was able to upload image if the image is stored in local directory. but the problem is that i have to upload image from another image url.
could anyone help me.
$localfile = "testing.jpg";
// echo filesize($localfile); die;
$url = API_URL . "pages/343/files/=".$localfile."?description=testing";
$fp = fopen ($localfile, "r");
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_USERPWD, API_USERNAME . ":" . API_PASSWORD);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
$http_result = curl_exec($ch);
$error = curl_error($ch);
$http_code = curl_getinfo($ch ,CURLINFO_HTTP_CODE);
// $jsonOutput = json_decode($http_result, true);
curl_close($ch);
fclose($fp);
i have tried to change $localfile to
$localfile = "https://someurl/someimage.png";
but it gave me following error.
Warning: curl_setopt(): cannot represent a stream of type tcp_socket/ssl as a STDIO FILE* in C:\xampp\htdocs\newPutMedia.php on line 29
Warning: filesize(): stat failed for https://help.optimizely.com/hc/en-us/article_attachments/200205465/1._Site-Wide_Addition_gmc_BEFORE.png in C:\xampp\htdocs\newPutMedia.php on line 30
Apparently this is a bug in PHP cURL. You can use the bug fix as mentioned here or you can do this :
Copy the file from URL to your local directory :
file_put_contents(__DIR_\_ . "/testing.jpg", file_get_contents($url));
Use the new file stored in your server for cURL PUT request :
$localfile = __DIR_\_ . "/testing.jpg";
Since the bug does not exist for file reading from your server, the bug won't cause errors and this would perfectly work.
I would like to download a file with Curl.
The problem is that the download link is not direct, for example:
http://localhost/download.php?id=13456
When I try to download the file with curl, it download the file download.php!
Here is my curl code:
###
function DownloadTorrent($a) {
$save_to = $this->torrentfolder; // Set torrent folder for download
$filename = str_replace('.torrent', '.stf', basename($a));
$fp = fopen ($this->torrentfolder.strtolower($filename), 'w+');//This is the file where we save the information
$ch = curl_init($a);//Here is the file we are downloading
curl_setopt($ch, CURLOPT_ENCODING, "gzip"); // Important
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_URL, $fp);
curl_setopt($ch, CURLOPT_HEADER,0); // None header
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); // Binary trasfer 1
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
Is there a way to download the file without knowing the path?
You may try CURLOPT_FOLLOWLOCATION
TRUE to follow any "Location: " header that the server sends as part
of the HTTP header (note this is recursive, PHP will follow as many
"Location: " headers that it is sent, unless CURLOPT_MAXREDIRS is
set).
So it will result into:
function DownloadTorrent($a) {
$save_to = $this->torrentfolder; // Set torrent folder for download
$filename = str_replace('.torrent', '.stf', basename($a));
$fp = fopen ($this->torrentfolder.strtolower($filename), 'w+');//This is the file where we save the information
$ch = curl_init($a);//Here is the file we are downloading
curl_setopt($ch, CURLOPT_ENCODING, "gzip"); // Important
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER,0); // None header
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); // Binary transfer 1
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
Set the FOLLOWLOCATION option to true, e.g.:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
Options are documented here: http://www.php.net/manual/en/function.curl-setopt.php
Oooh !
CURLOPT_FOLLOWLOCATION work perfect...
The problem is that I use CURLOPT_URL for fopen(), I simply change CURLOPT_URL whit CURLOPT_FILE
and it works very well!
thank you for your help =)
I am trying to do a simple cURL file upload from one server to another. The problem is I get Error #3 from the cUrl error codes: The URL was not properly formatted.
I have copied the url into my browser and logged onto the ftp site without a problem. I have also verified the proper formatting and searched the web and this site for an answer without any success.
Here's the code:
$ch = curl_init();
$localfile = '/home/httpd/vhosts/homeserver.com/httpdocs/admin.php';
echo $localfile; //This reads back to proper path to the file
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, 'ftp://username:password#199.38.215.1xx/');
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);
$error_no = curl_errno($ch);
curl_close ($ch);
if ($error_no == 0) {
$error = 'File uploaded succesfully.';
} else {
$error = 'Upload error:'.$error_no ;//Error codes explained here http://curl.haxx.se/libcurl/c/libcurl-errors.html';
}
echo $error;
I have also tried this:
curl_setopt($ch, CURLOPT_URL, 'ftp://199.38.215.1xx/');
curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
I still get error #3.
Any ideas?
your remote URL needs to contain the path and name of the destination file, as shown in this example
<?php
// FTP upload to a remote site Written by Daniel Stenberg
// original found at http://curl.haxx.se/libcurl/php/examples/ftpupload.html
//
// A simple PHP/CURL FTP upload to a remote site
//
$localfile = "me-and-my-dog.jpg";
$ftpserver = "ftp.mysite.com";
$ftppath = "/path/to";
$ftpuser = "myname";
$ftppass = "mypass";
$remoteurl = "ftp://${ftpuser}:${ftppasswd}#${ftpserver}${ftppath}/${localfile}";
$ch = curl_init();
$fp = fopen($localfile, "rb");
// we upload a JPEG image
curl_setopt($ch, CURLOPT_URL, $remoteurl);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
// set size of the image, which isn't _mandatory_ but helps libcurl to do
// extra error checking on the upload.
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
$error = curl_exec($ch);
// check $error here to see if it did fine or not!
curl_close($ch);
?>