如何从PHP调用网站服务?
作者:互联网
我的问题如下,我的服务器上有一个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>");
和
$stuff = file_get_contents("http://...<the url here>");
我正在考虑导入EmailReports.php但由于没有功能似乎不正确,它会自动发送电子邮件.
或者我可以复制EmailReports.php代码,但这违反了QA政策,因为需要额外的测试.
你能引导我一下吗?
提前致谢.
解决方法:
您可以使用Curl请求从任何网站检索信息(xml / html / json / etc).
什么是CURL? (简答)
PHP has a very powerful library of calls that are specifically designed to safely fetch data from remote sites. It’s called CURL.
资料来源:PHP, CURL, and YOU!
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';
}
资料来源: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)
用法
<?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请求(这是预期的).
标签:http-get,file-get-contents,php,web-services,callback 来源: https://codeday.me/bug/20191007/1867689.html