Exception handling for simulation request Tool Curl

Source: Internet
Author: User

in the daily development process, there are more scenarios for simulating requests using curl. When cross-site requests, there is no guarantee of the stability of other sites.
When other websites fail to access them, they can affect the current business system and are not easy to troubleshoot and therefore require exception handling.

Private Function Curlpost (string $route, array $postData): array
{
Try{
       //send message to task queue
   $url= env (' Api_url ') . $route;
       //initialization
   $curl= Curl_init();
       //Set the crawledURL
   curl_setopt($curl, Curlopt_url, $url);
       //set the header file information as the data stream output
   curl_setopt($curl, Curlopt_header, false);
       //The information obtained is returned in the form of a file stream, rather than as a direct output.
   curl_setopt($curl, Curlopt_returntransfer, true);
       //SetPostWay to submit
   curl_setopt($curl, Curlopt_post, true);
       //SetPostdata
   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 ";//SimulationWindowsNormal User access
   //curl_setopt ($curl, curlopt_useragent, $userAgent);
       //Execute command
   $output= curl_exec($curl);

Get error code
       $curlErrno= Curl_errno($curl);
       if($curlErrno) {
           throw NewException (Curl_error($curl) . '(' . $curlErrno. ')');
       }
       //CloseURLRequest
       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);
Verifying that the curl is sent properly
If(!$result[' Curl_status ']) {
   return$this->returnerror ($result[' Curl_message ']);
}

//Calibration Service
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 returns 0 (0) If no error occurs.

string    Curl_error ( resource $ch ) returns the text error details of the most recent CURL operation.

Exception handling for simulation request Tool Curl

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.