thinkphp - PHP 怎么处理curl异常?

查看:172
本文介绍了thinkphp - PHP 怎么处理curl异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

PHP是怎么处理异常的?像下面这样的代码,如何得知是执行成功了还是失败了?

    public function get_user($ch, $apikey) {
        \Think\Log::record('into get_user...');
        curl_setopt($ch, CURLOPT_URL, 'https://sms.xxx.com/v2/user/get.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('apikey' => $apikey)));

        $response = curl_exec($ch);
        \Think\Log::record('$response : '.$response);
        if (false === $response) {
            die(curl_error);
        }
        return $response;
    }

解决方案

$apiKey = '';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/user/get.json');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('apikey' => $apiKey)));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
$response = curl_exec($ch);
if (false === $response) {
    die(curl_error($ch));
}
print_r($response);

自己运行调试吧,不解释了。

这篇关于thinkphp - PHP 怎么处理curl异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆