0

My problem is the following, I have an EmailReports.php on my server which I use to send mails like EmailReports.php?who=some@gmail.com&what=123456.pdf

I can NOT modify EmailReports.php since that belongs to a diferent project and it instantly sends an email and has been aproved by QA team and all that stuff.

Now, on a diferent LookReports.php I need to offer a service like "Send me the reports I reviewed" which manually can be easily executed as just calling EmailReports.php, the question is, how can I do it by PHP code? so it calls the other PHP automatically.

I have tried without success:

$stuff = http_get("http://...<the url here>");

and

$stuff =  file_get_contents("http://...<the url here>");

I was thinking on import the EmailReports.php but does not seem right since there is no functions, it automatically sends an email.

Or I could replicate EmailReports.php code but that is against the QA policy since extra tests would be needed.

Could you guide me a bit?

Thanks in advance.

RandomGuy42
  • 301
  • 4
  • 14

1 Answers1

6

You could use a Curl request to retrieve information (xml/html/json/etc) from any website.

What is CURL? (short answer)

PHP has a very powerful library of calls that are specifically designed to safely fetch data from remote sites. It's called CURL.

Source : PHP, CURL, and YOU!

Example of Curl function in PHP

/* gets the data from a URL */
function get_data($url)
{
 if(function_exists('curl_init')){
 $ch = curl_init();
 $timeout = 5;
 curl_setopt($ch,CURLOPT_URL,$url);
 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
 curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
 $data = curl_exec($ch);
 curl_close($ch);
 return $data;
 } else 'curl is not available, please install';
 }

Source : Download a URL’s Content Using PHP cURL

Alternatively, you could do what you are currently doing with file_get_contents but many hosts don't allow this. (Walsh, 2007)

Usage

<?php
$mydata = get_data('http://www.google.co.nz');
echo '<pre>';
print_r($mydata); //display the contents in $mydata as preformatted text
echo '</pre>';
?>

Try to test it, with other websites because more often than not google will return a 404 request (this is to be expected), after a curl has been executed.

classicjonesynz
  • 4,012
  • 5
  • 38
  • 78
  • This does not work neither, I think it could be server-configuration related? – RandomGuy42 Jul 20 '12 at 04:15
  • Curl is now available by default with `PHP 5.4.3` [curl requirements](http://www.php.net/manual/en/curl.requirements.php), alternatively if you're running something like a windows system with `xammp` installed I'd look into this question [here](http://stackoverflow.com/questions/181082/how-do-i-install-curl-on-windows). Regards – classicjonesynz Jul 20 '12 at 04:19
  • I am using a remote Linux server with PHP 5.2.17, that may be the reason is not working, but I would expect an error at least. – RandomGuy42 Jul 20 '12 at 04:23
  • On a windows system, you will need to uncomment the line in `php.ini` regarding `libcurl.dll`. (Alternatively read [this](http://www.php.net/manual/en/curl.installation.php) from php.org regarding installing curl on a windows enviroment). As stated above in a `linux` system (based on the version of php installed) no install should be required. – classicjonesynz Jul 20 '12 at 04:24
  • PHP 5.0.0 requires a libcurl version 7.10.5 or greater. (packages are available [here](http://curl.haxx.se/)) – classicjonesynz Jul 20 '12 at 04:27
  • For previous versions of `PHP` refer to [this](http://curl.haxx.se/docs/install.html) document from the [curl website](http://curl.haxx.se/) on installation for linux systems. Also use `phpinfo()` to check if curl is available. Regards – classicjonesynz Jul 20 '12 at 04:32