thinkPHP 怎么处理curl异常?

php中文网
发布: 2016-06-06 20:09:16
原创
1497人浏览过

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;
    }
登录后复制

回复内容:

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);
登录后复制

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

抛出异常 throw new Exception()
可能触发异常的代码 try{...}
捕获异常 catch(Exception $e){}

补充:

立即学习PHP免费学习笔记(深入)”;

JoinMC智能客服
JoinMC智能客服

JoinMC智能客服,帮您熬夜加班,7X24小时全天候智能回复用户消息,自动维护媒体主页,全平台渠道集成管理,电商物流平台一键绑定,让您出海轻松无忧!

JoinMC智能客服 193
查看详情 JoinMC智能客服
protected function curl($url, $postFields = null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.1)');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch,CURLOPT_HTTPHEADER,array('Expect:'));
if (is_array($postFields) && 0 < count($postFields))
{
$postBodyString = '';
$postMultipart = false;
foreach ($postFields as $k => $v)
{
if('@' != substr($v, 0, 1))//判断是不是文件上传
{
$postBodyString .= '$k=' . urlencode($v) . '&';
}
else//文件上传用multipart/form-data,否则用www-form-urlencoded
{
$postMultipart = true;
}
}
unset($k, $v);
curl_setopt($ch, CURLOPT_POST, 1);
if ($postMultipart)
{
cur l_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
}
else
{
//var_dump($postBodyString);
curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
}
}
$reponse = curl_exec($ch);
//return curl_getinfo($ch);
if (curl_errno($ch))
{
throw new Exception(curl_error($ch),0);
}
else
{
$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (200 !== $httpStatusCode)
{
throw new Exception($reponse,$httpStatusCode);
}
}
curl_close($ch);
return $reponse;
}
登录后复制

补充 @incNick 的答案,PHP 7 版本,异常 多了异常类 Error ,跟 Exception 是平级关系
可以参考
1、http://php.net/manual/zh/migration70.incompatible.php
2、http://php.net/manual/zh/language.errors.php7.php

在一个项目里面, 你很难保证curl发出的http请求一定是正确并且在超时范围内返回的..反而是经常出问题的.
所以为了项目后面的代码能正确处理curl遇到的错误, 我认为抛出异常是最好的方式.

$response = curl_exec($ch);
if (false === $response) {
    die(curl_error($ch));
    throw new Exception(curl_error($ch),curl_errno($ch));
}
登录后复制

PHP 也有try catch throw吧

相关标签:
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号