0

在自定义 Joomla 2.5 模块中,我有这个:

$username   = $params->get('username'   ,   '');
$password   = $params->get('password'   ,   '');
$url    = $params->get('url'        ,   'http://api.xxxxx.com');

所以,我记得 config.xml 中的变量。

我写了这个函数:

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 正确填满了我的 url 之后。

谢谢

4

0 回答 0