flc1125 / alidayu

阿里大于(鱼)API接口-SDK

Home Page:http://flc.io/2016/09/563.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

报错Warning: current() expects parameter 1 to be array, null given in vendor\flc\alidayu\src\Alidayu\Client.php on line 129

xualen opened this issue · comments

代码如下:
require '/vendor/flc/alidayu/autoload.php';
use Flc\Alidayu\Client;
use Flc\Alidayu\App;
use Flc\Alidayu\Requests\AlibabaAliqinFcSmsNumSend;
use Flc\Alidayu\Requests\IRequest;

// 配置信息
$config = array(
'app_key' => '......',
'app_secret' => '......'
);

// 使用方法一
$client = new Client(new App($config));
$req = new AlibabaAliqinFcSmsNumSend;

$req->setRecNum('13000000000')
->setSmsParam([
'number' => rand(100000, 999999)
])
->setSmsFreeSignName('...')
->setSmsTemplateCode('SMS_10000000');

$resp = $client->execute($req);
报错:
Warning: current() expects parameter 1 to be array, null given in E:\web\vendor\flc\alidayu\src\Alidayu\Client.php on line 129

你的请求返回值是Null,json解析空也是Null,然后用来current取值就报参数应该是个数组,不能用Null值。

是的。一般出現這種情況,是網絡原因導致的。建議在最外層增加一個try catch

  • 我也是这个错误:current() expects parameter 1 to be array, null given
     * 解析返回数据
     * @return array|false
     */
    protected function parseRep($response)
    {
        if ($this->format == 'json') {
            $resp = json_decode($response);

            if (false !== $resp) {
                $resp = current($resp);
            }
        }

        elseif ($this->format == 'xml') {
            $resp = @simplexml_load_string($response);
        }

        else {
            throw new Exception("format错误...");
  • 加上 try catch 同样一样的,返回错误信息:current() expects parameter 1 to be array, null given
    protected function parseRep($response)
    {
        try {
            if ($this->format == 'json') {
                $resp = json_decode($response);
                if (false !== $resp) {
                    $resp = current($resp);
                }
            } elseif ($this->format == 'xml') {
                $resp = @simplexml_load_string($response);
            } else {
                throw new Exception("format错误...");
            }
        } catch (\Exception $e) {
            $resp = $e->getMessage();
        }

        return $resp;
    }
  • @flc1125 这个有什么办法可以解决吗?公司里是好的,结果回家就会出现这个情况

@xualen 这个问题你解决了没有?

这块我重新发布一个版本专门修复下吧!

已修复。已更新packagist