如何从PHP调用网站服务


How to call a website service from PHP?

我的问题是,我的服务器上有一个EmailReports.php,我用它来发送电子邮件,比如EmailReports.php?谁=some@gmail.com&what=12346.pdf

我不能修改EmailReports.php,因为它属于一个不同的项目,它会立即发送一封电子邮件,并得到了QA团队和所有这些东西的批准。

现在,在一个不同的LookReports.php上,我需要提供一个类似"向我发送我审查过的报告"的服务,它可以像调用EmailReports.php一样手动执行,问题是,我如何通过php代码来执行?因此它会自动调用另一个PHP。

我尝试过但没有成功:

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

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

我想导入EmailReports.php,但似乎不对,因为没有任何功能,它会自动发送电子邮件。

或者我可以复制EmailReports.php代码,但这违反了QA政策,因为需要额外的测试。

你能给我介绍一下吗?

提前谢谢。

您可以使用Curl请求从任何网站检索信息(xml/html/json/etc)。

什么是CURL?(简短回答)

PHP有一个非常强大的调用库,专门用于从远程站点安全地获取数据。它被称为CURL。

来源:PHP,CURL,和你!

PHP中Curl函数的示例

/* 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';
 }

来源:使用PHP cURL下载URL的内容

或者,您可以使用file_get_contents执行当前正在执行的操作,但许多主机不允许这样做。(Walsh,2007)

用法

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

试着用其他网站测试它,因为在执行curl之后,google通常会返回404 request(这是意料之中的)。