在自定义的Joomla 2.5模块中,我有这个:
$username = $params->get('username' , '');
$password = $params->get('password' , '');
$url = $params->get('url' , 'http://api.xxxxx.com');
所以,我记得来自config的变量。
我写过这个函数:
function callAPI($data) {
global $username, $password, $url;
$data['username'] = $username;
$data['password'] = $password;
$data['output'] = 'XML';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$result = curl_exec($ch);
curl_close ($ch);
$parsed_result = new SimpleXMLElement(str_replace('&','&',$result));
return $parsed_result;
} // function callAPI($data)
而且......它不起作用......错误是
PHP Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML
如果手动我在功能中添加$ url ......它完成了他的工作:
function callAPI($data) {
global $username, $password, $url;
$url = 'http://api.xxxxxx.com';
$data['username'] = $username;
$data['password'] = $password;
$data['output'] = 'XML';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$result = curl_exec($ch);
curl_close ($ch);
$parsed_result = new SimpleXMLElement(str_replace('&','&',$result));
return $parsed_result;
} // function callAPI($data)
显然,我确信$ username和$ password都填充了正确的值,因为如果我添加$ url,我的网络服务器会响应。所以我怀疑只有一个问题是“url”值。
我需要解决我的问题。在函数“callAPI”中,$ url为空。在片段的第一部分,$ params-> get $ url正确填满我的网址。
谢谢