thinkPHP 怎麼處理curl異常?

來源:互聯網
上載者:User
關鍵字 thinkphp php 異常
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){}

補充:

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吧

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.