如何从PHP调用网站服务? [英] How to call a website service from PHP?
问题描述
我的问题是以下,我有一个EmailReports.php在我的服务器上,我用来发送电子邮件像EmailReports.php?who=some@gmail.com& what = 123456.pdf
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
我不能修改EmailReports.php,因为它属于一个不同的项目,它立即发送一封电子邮件,并已被质量保证小组和所有这些东西。
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.
现在,在一个不同的LookReports.php我需要提供像发送我的报告我审查的服务,手动可以轻松地执行只是调用EmailReports.php,问题是,我该怎么做PHP代码?所以它自动调用其他PHP。
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.
我没有成功尝试:
$stuff = http_get("http://...<the url here>");
和
$stuff = file_get_contents("http://...<the url here>");
我正在考虑导入EmailReports.php,但似乎不正确,因为没有函数,它自动发送电子邮件。
I was thinking on import the EmailReports.php but does not seem right since there is no functions, it automatically sends an email.
或者我可以复制EmailReports.php代码,但这是违反QA政策,因为需要额外的测试。
Or I could replicate EmailReports.php code but that is against the QA policy since extra tests would be needed.
您能帮我指点一下吗?
提前感谢。
推荐答案
您可以使用 Curl 请求来检索信息(xml / html / json / etc)。
You could use a Curl request to retrieve information (xml/html/json/etc) from any website.
什么是CURL? (简短答案)
PHP有一个非常强大的调用库,专门用于从远程站点安全地获取数据。
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和您!
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)
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>';
?>
尝试测试它与其他网站,因为更多的时候 google
将在执行curl后返回 404请求
(这是预期的)。
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.
这篇关于如何从PHP调用网站服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!