【发布时间】:2016-10-17 19:06:42
【问题描述】:
我最近使用我的 php 从程序性转向 oop,我正在从一本书中学习,除了做书中的练习和项目,我还给自己一个真实的项目来为我自己的网站做。
我想在我的网站上实现一个 sms 功能,在研究项目时遇到了这个连接到我将使用的公司的 API 的小型类库,该库位于 github 上,似乎不受支持,我的电子邮件开发人员正在反弹。
这是library,这是我用来初始化它的代码:
require_once('classes/class.smsquick.php');
$username = "someuser";
$password = "somepass";
$api = new SmsQuick($username, $password);
//uncommenting the line below returns undefined_index @ line 312 and 282
//$available_credits = $api->checkBalance();
//var_dump($api); //used for checking
这些是我收到的错误:
注意:未定义的偏移量:C:\websites\ooptuts\public\classes\class.smsquick.php 第 312 行中的 1
警告:array_values() 期望参数 1 是数组,布尔值在 C:\websites\ooptuts\public\classes\class.smsquick.php 第 282 行给出
在接下来的两个方法中指出了第 312 和 282 行,最后两个方法是必需的,所以我也展示了它们:
public function checkBalance() {
$vars = array(
'username' => $this->api_username,
'password' => $this->api_password,
'action' => 'balance',
);
$retval = $this->executeApiRequest($vars);
list(, $response) = array_values(reset($retval)); // line 282
return (int) $response;
}
/**
* Helper method to execute an API request.
*
* @param array $vars
* Data to POST to SMS gateway API endpoint.
*
* @return array
* Response from SMS gateway.
*/
public function executeApiRequest($vars) {
// Basic validation on the authentication details
foreach ($vars as $key => $value) {
switch ($key) {
case 'username':
case 'password':
if (empty($value)) {
throw new Exception('API username or password not specified.');
}
break;
}
}
$data = $this->preparePostData($vars);
$retval = $this->executePostRequest($data);
list($status, $response) = explode(':', $retval); // line 312
if ($status == 'ERROR') {
throw new Exception(strtr('There was an error with this request: !error.', array('!error' => $response)));
}
$data = array();
$lines = explode("\n", $retval);
foreach (array_filter($lines) as $i => $line) {
$line = trim($line);
$data[$i] = explode(':', $line);
}
return $data;
}
protected function preparePostData($data) {
$post_data = array();
foreach ($data as $key => $value) {
switch ($key) {
case 'to':
// Support multiple phone numbers.
$value = implode(',', array_unique($value));
break;
}
$post_data[] = $key . '=' . rawurlencode($value);
}
return implode('&', $post_data);
}
protected function executePostRequest($data) {
$ch = curl_init($this->api_endpoint);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$retval = curl_exec($ch);
curl_close($ch);
return $retval;
}
问题是由我如何初始化课程还是我忽略了什么引起的?.. 我似乎花了两天时间在谷歌等圈子里跑来跑去,并没有取得太大进展,希望得到其他人的一些建议。
谢谢
【问题讨论】: