PHP - How to check if Curl actually post/send request? PHP - How to check if Curl actually post/send request? curl curl

PHP - How to check if Curl actually post/send request?


You can use curl_getinfo() to get the status code of the response like so:

// set up curl to point to your requested URL$ch = curl_init($fullcurl);// tell curl to return the result content instead of outputting itcurl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);// execute the request, I'm assuming you don't care about the result contentcurl_exec($ch);if (curl_errno($ch)) {    // this would be your first hint that something went wrong    die('Couldn\'t send request: ' . curl_error($ch));} else {    // check the HTTP status code of the request    $resultStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);    if ($resultStatus == 200) {        // everything went better than expected    } else {        // the request did not complete as expected. common errors are 4xx        // (not found, bad request, etc.) and 5xx (usually concerning        // errors/exceptions in the remote script execution)        die('Request failed: HTTP status code: ' . $resultStatus);    }}curl_close($ch);

For reference: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

Or, if you are making requests to some sort of API that returns information on the result of the request, you would need to actually get that result and parse it. This is very specific to the API, but here's an example:

// set up curl to point to your requested URL$ch = curl_init($fullcurl);// tell curl to return the result content instead of outputting itcurl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);// execute the request, but this time we care about the result$result = curl_exec($ch);if (curl_errno($ch)) {    // this would be your first hint that something went wrong    die('Couldn\'t send request: ' . curl_error($ch));} else {    // check the HTTP status code of the request    $resultStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);    if ($resultStatus != 200) {        die('Request failed: HTTP status code: ' . $resultStatus);    }}curl_close($ch);// let's pretend this is the behaviour of the target serverif ($result == 'ok') {    // everything went better than expected} else {    die('Request failed: Error: ' . $result);}


in order to be sure that curl sends something, you will need a packet sniffer.You can try wireshark for example.

I hope this will help you,

Jerome Wagner