In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to simulate the exception handling of the request tool curl, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
In the daily development process, there are many scenarios that use curl to simulate requests. When cross-site requests, there is no guarantee of the stability of other sites. When other websites can not be accessed smoothly, it will affect the current business system, and it is not easy to troubleshoot the problem, so exception handling is needed. Private function curlPost (string $route, array $postData): array {try {/ / send messages to the task queue $url = env ('API_URL'). $route; / / initialize $curl = curl_init (); / / set the crawled url curl_setopt ($curl, CURLOPT_URL, $url); / / set the header file information as a data stream output curl_setopt ($curl, CURLOPT_HEADER, false); / / set the information obtained to be returned in the form of a file stream rather than direct output. Curl_setopt ($curl, CURLOPT_RETURNTRANSFER, true); / / set post to submit curl_setopt ($curl, CURLOPT_POST, true); / / set post data curl_setopt ($curl, CURLOPT_POSTFIELDS, $postData); / / $userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36" / / simulate the normal access of windows users / / curl_setopt ($curl, CURLOPT_USERAGENT, $userAgent); / / execute the command $output = curl_exec ($curl); / / get the error code $curlErrno = curl_errno ($curl); if ($curlErrno) {throw new Exception ($curl). '('. $curlErrno. ')');} / close URL request curl_close ($curl); $result = json_decode ($output, true); $result ['curl_status'] = 1; $result [' curl_message'] ='';} catch (\ Exception $e) {$result ['curl_status'] = 0; $result [' curl_message'] = $e-> getMessage ();} return $result }
/ / call
$result = $this- > curlPost ($route, $postData); / / verify whether curl sends normal if (! $result ['curl_status']) {return $this- > returnError ($result [' curl_message']);} / / verify business if ($result ['errorCode'] = = 0) {return $this- > returnSuccess ([]);} else {return $this- > returnError ($result [' errorMsg']);}
Curl correlation function
Int curl_errno (resource $ch) returns the error code for the last cURL operation.
Returns an error code or 0 (zero) when no error occurs.
String curl_error (resource $ch) returns the text error details of the most recent cURL operation.
The above is all the contents of the article "how to simulate the exception handling of the request tool curl". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.