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