如何从PHP调用网站服务?

Ran*_*y42 0 php web-services http-get callback file-get-contents

我的问题是下面,我有一个EmailReports.php我的服务器上,我用它来发送类似EmailReports.php?who=some@gmail.com&what=123456.pdf邮件

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

现在,在一个不同的LookReports.php上我需要提供一个服务,比如"发给我我回复的报告",只需调用EmailReports.php即可轻松执行,问题是,如何通过PHP代码完成?所以它会自动调用其他PHP.

我试过没有成功:

$stuff = http_get("http://...<the url here>");
Run Code Online (Sandbox Code Playgroud)

$stuff =  file_get_contents("http://...<the url here>");
Run Code Online (Sandbox Code Playgroud)

我正在考虑导入EmailReports.php但由于没有功能似乎不正确,它会自动发送电子邮件.

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

你能引导我一下吗?

提前致谢.

Kil*_*awr 6

您可以使用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';
 }
Run Code Online (Sandbox Code Playgroud)

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

或者,您可以执行当前正在执行的操作,file_get_contents但许多主机不允许这样做.(沃尔什,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>';
?>
Run Code Online (Sandbox Code Playgroud)

尝试使用其他网站进行测试,因为在执行curl之后,通常google会返回a 404 request(这是预期的).