1

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);
3
  • could you show us your php code? Commented Jan 13, 2015 at 23:24
  • and $result is string even when status is 400 ? Commented Jan 13, 2015 at 23:33
  • $results is just a raw server response - I need to parse this raw data Commented Jan 13, 2015 at 23:40

1 Answer 1

7

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

Sign up to request clarification or add additional context in comments.

8 Comments

If server response is 400 (error), that means user submitted wrong info. In that case I need to extract error message, and ECHO this error message to user. So I 1st need to identify if I got 200 or 400 status code. 2nd, if i got 400, then I need to ECHO just the error message
so? did you try my changes? did you get $header?
Here is what I get if I use your code and do echo $result - string(207) "HTTP/1.0 200 OK Server: Apache/2.2.15 (CentOS) Vary: Accept-Encoding,User-Agent Content-Type: text/html; charset=UTF-8 Date: Wed, 14 Jan 2015 00:07:32 GMT Connection: Keep-Alive Content-Length: 139 " string(139) "2517111200" HTTP/1.0 200 OK Server: Apache/2.2.15 (CentOS) Vary: Accept-Encoding,User-Agent Content-Type: text/html; charset=UTF-8 Date: Wed, 14 Jan 2015 00:07:32 GMT Connection: Keep-Alive Content-Length: 139 2517111200
check my updates, and now you can see the difference between curl response and 'server row response'
here is waht I get for echo $header - string(211) "HTTP/1.0 400 Bad Request Server: and a bunch of other code. ... so it does not work because I get too much unnecessary code --- I need to clean up the response I get ...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.