Page 1 of 1

Grab a File & Put It On My Server

Posted: Sun Nov 08, 2015 7:41 am
by transfield
Hi,
I'm using the following code to grab a csv file from one website & keep it on my website. The code is working fine.

Code: Select all

<?php 
$url  = 'http://thewebsite.com/export.aspx?type=csv&report=nameofthereport~8~';
    $path = '/home/path/to/mywebsite.com/public_ftp/incoming/nameofthereport_Area_8.csv';
    $fp = fopen($path, 'w');

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FILE, $fp);

    $data = curl_exec($ch);

    curl_close($ch);
    fclose($fp);
?>
Here's what I want to do next. I want to query a database, and replace nameofthereport and 8 with the results of the query. My code to query the database is below.

Code: Select all

<?php 
include("/home/path/to/mywebsite.com/public_html/db_connect.php"); //connect to the database
$today = date('Y-m-d');
$query1=("SELECT * FROM `table_name` WHERE expiry > $today");
$result1=mysqli_query($GLOBALS["___mysqli_ston"], $query1);
$num1=mysqli_num_rows($result1); 

if ($num1 > 0) { 
while ($data = mysqli_fetch_array($result1)){ 
$email_to = $data["email"]; 
$district = $data["district"];
$report = $data["report"];
                      }
                                                                      }
?>
Based on the code above, I would like to replace nameofthereport with $report and 8 with $district. This means that the script first queries the database, inserts the query results of $report and $district into $url, grabs the csv file and stores it in $path

Could you show me the code please? Thanks.

Re: Grab a File & Put It On My Server

Posted: Sun Nov 08, 2015 8:05 am
by Celauran
Something like this should work

Code: Select all

<?php
function getRemoteReport($report_name, $district) {
    $base_url  = 'http://thewebsite.com/export.aspx?type=csv&report=';
    $base_path = '/home/path/to/mywebsite.com/public_ftp/incoming/';

    $url  = "{$base_path}{$report_name}~{$district}~";
    $path = "{$base_path}{$report_name}_Area_{$district}.csv";

    $fp = fopen($path, 'w');
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FILE, $fp);

    $data = curl_exec($ch);

    curl_close($ch);
    fclose($fp);
}
?>

<?php
include("/home/path/to/mywebsite.com/public_html/db_connect.php"); //connect to the database
$today = date('Y-m-d');
$query1=("SELECT * FROM `table_name` WHERE expiry > $today");
$result1=mysqli_query($GLOBALS["___mysqli_ston"], $query1);
$num1=mysqli_num_rows($result1);

if ($num1 > 0) {
    while ($data = mysqli_fetch_array($result1)){
        $email_to = $data["email"];
        $district = $data["district"];
        $report = $data["report"];
        getRemoteReport($report, $district);
    }
}
?>
Note that getRemoteReport is being called from inside a loop, so if it's going to be fetching many reports, it's going to be slow and you may want to explore alternatives at that point.

Re: Grab a File & Put It On My Server

Posted: Sun Nov 08, 2015 9:12 am
by transfield
Wonderful help, I must say.

I had to change:

Code: Select all

$url  = "{$base_path}{$report_name}~{$district}~";
to

Code: Select all

$url  = "{$base_url}{$report_name}~{$district}~";


in order for it to work & after that it worked like a charm.

Thank you very much Celauran. God bless you :-)

Re: Grab a File & Put It On My Server

Posted: Sun Nov 08, 2015 9:17 am
by Celauran
As, yes. Wrote it up quickly and had a few mistakes. Glad you got it working.