How does thinkPHP handle curl exceptions?

Source: Internet
Author: User
How does PHP handle exceptions? How do I know whether the execution is successful or failed? {Code...} How does PHP handle exceptions? How do I know whether the execution is successful or failed?

    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;    }

Reply content:

How does PHP handle exceptions? How do I know whether the execution is successful or failed?

    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);

Run the debugging on your own. Do not explain it.

Throw Exception throw new Exception ()
Code that may trigger an exception try {...}
Catch (Exception $ e ){}

Supplement:

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 CT: '); if (is_array ($ postFields) & 0 <Count ($ postFields) {$ postBodyString = ''; $ postMultipart = false; foreach ($ postFields as $ k => $ v) {if ('@'! = Substr ($ v, 0, 1) // determines whether the file is uploaded {$ postBodyString. = '$ k = '. urlencode ($ v ). '&';} else // multipart/form-data for file upload; otherwise, 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 ;}

Supplement: @ incNick: in PHP 7, an Exception Error is added, which is in the same level as Exception.
Refer
1. http://php.net/manual/zh/migration70.incompatible.php
2. http://php.net/manual/zh/language.errors.php7.php

In a project, it is hard to ensure that the http requests sent by curl are correct and returned within the timeout range.
So for the code behind the project to correctly handle curl errors, I think throwing an exception is the best way.

$response = curl_exec($ch);if (false === $response) {    die(curl_error($ch));    throw new Exception(curl_error($ch),curl_errno($ch));}

PHP also has try catch throw.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.