当前位置:Gxlcms > PHP教程 > thinkPHP怎么处理curl异常?

thinkPHP怎么处理curl异常?

时间:2021-07-01 10:21:17 帮助过:27人阅读

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

  1. <code> public function get_user($ch, $apikey) {
  2. \Think\Log::record('into get_user...');
  3. curl_setopt($ch, CURLOPT_URL, 'https://sms.xxx.com/v2/user/get.json');
  4. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('apikey' => $apikey)));
  5. $response = curl_exec($ch);
  6. \Think\Log::record('$response : '.$response);
  7. if (false === $response) {
  8. die(curl_error);
  9. }
  10. return $response;
  11. }</code>

回复内容:

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

  1. <code> public function get_user($ch, $apikey) {
  2. \Think\Log::record('into get_user...');
  3. curl_setopt($ch, CURLOPT_URL, 'https://sms.xxx.com/v2/user/get.json');
  4. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('apikey' => $apikey)));
  5. $response = curl_exec($ch);
  6. \Think\Log::record('$response : '.$response);
  7. if (false === $response) {
  8. die(curl_error);
  9. }
  10. return $response;
  11. }</code>

  1. <code>$apiKey = '';
  2. $ch = curl_init();
  3. curl_setopt($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/user/get.json');
  4. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('apikey' => $apiKey)));
  5. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  6. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
  7. $response = curl_exec($ch);
  8. if (false === $response) {
  9. die(curl_error($ch));
  10. }
  11. print_r($response);</code>

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

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

补充:

  1. <code>protected function curl($url, $postFields = null)
  2. {
  3. $ch = curl_init();
  4. curl_setopt($ch, CURLOPT_URL, $url);
  5. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.1)');
  6. curl_setopt($ch, CURLOPT_HEADER, 0);
  7. curl_setopt($ch, CURLOPT_FAILONERROR, 0);
  8. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  9. //curl_setopt($ch,CURLOPT_HTTPHEADER,array('Expect:'));
  10. if (is_array($postFields) && 0 < count($postFields))
  11. {
  12. $postBodyString = '';
  13. $postMultipart = false;
  14. foreach ($postFields as $k => $v)
  15. {
  16. if('@' != substr($v, 0, 1))//判断是不是文件上传
  17. {
  18. $postBodyString .= '$k=' . urlencode($v) . '&';
  19. }
  20. else//文件上传用multipart/form-data,否则用www-form-urlencoded
  21. {
  22. $postMultipart = true;
  23. }
  24. }
  25. unset($k, $v);
  26. curl_setopt($ch, CURLOPT_POST, 1);
  27. if ($postMultipart)
  28. {
  29. cur l_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  30. }
  31. else
  32. {
  33. //var_dump($postBodyString);
  34. curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
  35. }
  36. }
  37. $reponse = curl_exec($ch);
  38. //return curl_getinfo($ch);
  39. if (curl_errno($ch))
  40. {
  41. throw new Exception(curl_error($ch),0);
  42. }
  43. else
  44. {
  45. $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  46. if (200 !== $httpStatusCode)
  47. {
  48. throw new Exception($reponse,$httpStatusCode);
  49. }
  50. }
  51. curl_close($ch);
  52. return $reponse;
  53. }</code>

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

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

  1. <code class="php">$response = curl_exec($ch);
  2. if (false === $response) {
  3. die(curl_error($ch));
  4. throw new Exception(curl_error($ch),curl_errno($ch));
  5. }</code>

PHP 也有try catch throw吧

人气教程排行