Get file content via PHP cURL
echo file_get_contents('http://www.adserversite.com/ads.php');
Who needs curl for this simple task?
PHP - file get contents using CURL
With curl, it's often a good idea to use curl_getinfo to obtain additional information about your curl connection before closing it. In your case, the NULL/FALSE/empty result could be due to a number of reasons and inspecting the curl info might help you find more detail. Here's a modified version of your function that uses this function to obtain additional information. You might consider writing print_r($info, TRUE)
to a log file or something. It might be empty because the server response is empty. It might be false because the url cannot be reached from your server due to firewall issues. It might be returning an http_code that is 404 NOT FOUND or 5XX.
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_ENCODING, 0);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST , "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$data = curl_exec($ch);
$info = curl_getinfo($ch);
if(curl_errno($ch)) {
throw new Exception('Curl error: ' . curl_error($ch));
}
curl_close($ch);
if ($data === FALSE) {
throw new Exception("curl_exec returned FALSE. Info follows:\n" . print_r($info, TRUE));
}
return $data;
}
EDIT: I added curl_errno checking too.
How to use CURL instead of file_get_contents?
try this:
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
cURL : Display/get text file contents using FTP with PHP
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;
Fetching contents of a file that was sent through cURL POST
For the first part before the update, the problem exists because when posting a file to a page, regardless of the method in which it happens (ie <input type='file />
, curl
, etc) it is always available to PHP via the $_FILES
variable, which is an associative array, in your case, should be $_FILES['array']
containing the information of the temporary file location. The array should be similar to:
Array
(
[array] => Array
(
[name] => array.txt
[type] => encoding type
[tmp_name] => /tmp/path/file
[error] => 0 (if no error)
[size] => [filesize]
)
)
From there, to access the file, you'd want to move it from the tmp
directory to one you have permission to access. This is accomplished with move_uploaded_file
. An example of usage with this would be:
$upDir = 'uploads/';
move_uploaded_file($_FILES['array']['tmp_name'], $upDir. $_FILES['array']['name']);
From there, the file will be on the server under the relative path to the php file in uploads/array.txt
, and you can do what you will with it there. ^^
php curl to download files
If i interpret this correct you want to build something like a cache where you download file x once from a remote server and after that the file should be served from your own server ?
If that is the case this could be a good approach for the problem:
class Downloader
{
private $path;
public $filename;
public function __construct()
{
$this->path = dirname(__FILE__);
}
public function getFile($url)
{
$fileName = $this->getFileName($url);
if(!file_exists($this->path."/".$fileName)){
$this->downloadFile($url,$fileName);
}
return file_get_contents($this->path."/".$fileName);
}
public function getFileName($url)
{
$slugs = explode("/", $url);
$this->filename = $slugs[count($slugs)-1];
return $this->filename;
}
private function downloadFile($url,$fileName)
{
//This is the file where we save the information
$fp = fopen ($this->path.'/'.$fileName, 'w+');
//Here is the file we are downloading, replace spaces with %20
$ch = curl_init(str_replace(" ","%20",$url));
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
// write curl response to file
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// get curl response
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
}
$downloader = new Downloader();
$content = $downloader->getFile("YOUR URL");
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$downloader->filename.'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . mb_strlen($content));
print $content;
exit;
This is a very basic concept there are many point this have to be improved and customized for your use case.
Related Topics
Secure User Image Upload Capabilities in PHP
MySQL or PHP Is Appending a  Whenever the £ Is Used
Woocommerce - How to Create Multiple Single Product Template Based on Category
How to Show PHP-Files as Plain Text in Apache
Magento - Redirect Customer from Observer Method
Is Preventing Xss and SQL Injection as Easy as Does This
Multiple Forms and One Processing Page
Downloading a Folder Through with Ftp Using PHP
How to Fetch All the Row of the Result in PHP MySQL
Php: Regular Expression to Get a Url from a String
Customize Addresses Fields on Woocommerce My Account and Checkout
Use PHP Namespace Inside Function
PHP Date Format Yyyy-Mm-Dd Minus or Add One Week from Now
Code to Parse User Agent String