Get file in PHP which is generated when visiting URL - php

In PHP, how do I get a file that is generated when you visit a URL. To explain this, if you visit this Google Drive URL:
https://docs.google.com/document/d/1ZUqkdVQZqpHhE25m-5wxhN1uzK7EvoZ81bmrwiwk3CY/export?format=doc
Download will start. How do I grab that download file in PHP?

Though in theory file_get_contents should have worked for this situation it did not for me. I'm behind proxy.
To get more control I had to use curl
I left proxy configuration as a part of code but commented it out.
<?php
$url = "https://docs.google.com/document/d/1ZUqkdVQZqpHhE25m-5wxhN1uzK7EvoZ81bmrwiwk3CY/export?format=doc";
$fileToSave = dirname(__FILE__) . '/localfile.doc';
// $proxy = "127.0.0.1:8080";
set_time_limit(0);
//This is the file where we save the information
$fp = fopen ($fileToSave, 'w+');
//Here is the file we are downloading.
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
// write curl response to file
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// Proxy stuff if you need it
// curl_setopt($ch, CURLOPT_PROXY, $proxy);
// ssl config here
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
// get curl response
if(curl_exec($ch) === false) {
echo 'Curl error: ' . curl_error($ch);
} else {
echo 'Done';
}
curl_close($ch);
fclose($fp);

$data=file_get_contents("https://docs.google.com/document/d/1ZUqkdVQZqpHhE25m-5wxhN1uzK7EvoZ81bmrwiwk3CY/export?format=doc") ;
Use this method to get the file content, I have used this method on a url that returned a CSV file upon hitting.

Related

How to get zip file from url using CURL request?

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);

PHP curl downloading a Zipped CSV from a caspio driven website

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();

how to access xml file of ssl or https type domain and store in our server using php

I want to access an XML file stored on another domain. The domain has an SSL certificate. I want to store it in my server folder. How can I achieve this in PHP? I am able to download without HTTPS from other sites, but unable to do it with HTTPS.
<?PHP
$dom = new DOMDocument();
$dom->load('www.123.com/abc.xml');
$dom->save('filename.xml');
?>
We are going to use Curl to download this file. There are 2 option.
The quick fix
The proper fix
The quick fix, first.
Warning: this can introduce security issues that SSL is designed to protect against.
set:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); to false
<?PHP
$url = "https://data.gov.in/sites/default/files/Date-Wise-Prices-all-Commodity.xml";
// create curl resource
$ch = curl_init();
//Now set some options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Download the given URL, and return output
$xml = curl_exec($ch);
// Check for errors
if (curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
}
// Close the cURL resource, and free system resources
curl_close($ch);
// Write the string $xml to a file, data.xml
if (file_put_contents ('data.xml', $xml) !== false) {
echo 'Success!';
} else {
echo 'Failed';
}
?>
The second, and proper fix. Set 3 options:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . '\CAcert.crt');
The last thing you need to do is download the CA certificate.
Go to, - http://curl.haxx.se/docs/caextract.html -> click 'cacert.pem' -> copie/paste the text in to a text editor -> save the file as 'CAcert.pem' Check it isn't 'CAcert.pem.txt'
<?PHP
$url = "https://data.gov.in/sites/default/files/Date-Wise-Prices-all-Commodity.xml";
// create curl resource
$ch = curl_init();
//Now set some options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . '\CAcert.crt');
// Download the given URL, and return output
$xml = curl_exec($ch);
// Check for errors
if (curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
}
// Close the cURL resource, and free system resources
curl_close($ch);
// Write the string $xml to a file, data.xml
if (file_put_contents ('data.xml', $xml) !== false) {
echo 'Success!';
} else {
echo 'Failed';
}
?>

how to get image from url in php?

I have one remote url which outputs the image
The url is in format like this
http://domain.com/my_file/view/<file_id>/FULL/
In the url "my_file" is a controller name, "view" is a function name and the other two are the parameters
If I hit this url in browser it shows me image
I want to take that image in my projects folder
I have tried with file_get_contents but it gives me warning with 404
How can I achieve that?
$img=file_get_contents('http://example.com/image/test.jpg');
file_put_contents('/your/project/folder/imgname.jpg',$img);
This works only if allow_url_fopen is set to 1 in your php.ini file.
If you can change this value, enable it and you're done.
Another option is CURL. Check if this module is enabled in your PHP configuration.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com/image/test.jpg');
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HEADER , 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = #curl_exec($ch);
$curl_err = curl_error($ch);
curl_close($ch);
if (empty($curl_err)) {
file_put_contents('/your/project/folder/imgname.jpg',$result);
}
If CURL is not enabled, your chance is to write a simple HTTP client like this:
$buf='';
$fp = fsockopen('example.com',80);
fputs($fp, "GET /image/test.jpg HTTP/1.1\n" );
fputs($fp, "Host: example.com\n" );
fputs($fp, "Connection: close\n\n" );
while (!feof($fp)) {
$buf .= fgets($fp,128);
}
fclose($fp);
file_put_contents('/your/project/folder/imgname.jpg',$buf);
Use Curl for this:
function curlFile($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
function readurl()
{
$url="http://domain.com/my_file/view/<file_id>/FULL/";
curlFile($url);
}
echo readurl();
If you are trying
$image = file_get_contents('http://domain.com/my_file/view/<file_id>/FULL/');
if($image) file_put_contents('some/folder/<file_id>');
and it does not work, it probably means either:
That there is access control that prevents it on the remote server. In that case, you must set the appropriate cookies. I suggest using curl to do that.
The image path is wrong; try to view source when navigating to http://domain.com/my_file/view/<file_id>/FULL/ using your browser.
The trailing / in your URL should not be there, e.g. http://domain.com/my_file/view/<file_id>/FULL?

PHP cURL error The URL was not properly formatted

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);
?>

Categories