Reputation: 506
I'm not a programmer, and i need your help please.
I have a cURL function that returns a server response - either success or error:
400 - Error Response:
<?xml version="1.0" encoding="UTF-8"?><statusCode>400</statusCode>
<errorMessage>In order to be contacted, please enter a valid phone number.</errorMessage>
200 - Success Response:
<?xml version="1.0" encoding="UTF-8"?>
<statusCode>200</statusCode>
I need to validate response based on 200 or 400 StatusCode.
If status is 200, proceed as normal.
If status is 400 (error) then I need to eztract the error messag (strip out all tags) and ECHO error message only (in the example above - In order to be contacted, please enter a valid phone number.)
How do I do it? Please help.
Thank you.
CURL Code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
Upvotes: 1
Views: 2337
Reputation: 17289
I am still not sure what exactly you are looking for but you can try:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($httpCode == 400) {
/* Handle 400 here. */
echo "Error: " . curl_error($ch);
}
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($result , 0, $header_size);
$body = substr($result , $header_size);
var_dump($header);
var_dump($body);
curl_close($ch);
you are very welcome if any questions
Upvotes: 7