3

As part of the registration for my Drupal 7 site I require the user to enter a valid phone number. Rather than doing only simple validation, I use the Numverify.com API to verify the format of the number on a per country basis and obtain extra information. This is done via a curl request which returns a json object.

The other day the API was unreachable for several hours and due to my lack of foresight, users were unable to register until I circumvented the API request.

To prevent this in the future, I need to be able to properly handle error codes such as 408. Would anybody be able to tell me how I can replicate a timeout request, locally or otherwise, for testing purposes please?

Jimmyb_1991
  • 346
  • 5
  • 12

4 Answers4

3

make a php page with the content: <?php http_response_code(408); and fetch that with curl.

hanshenrik
  • 19,904
  • 4
  • 43
  • 89
  • This is exactly what I was looking for although it took me a while to figure out how to use it for what I wanted. Thanks :D – Jimmyb_1991 Jan 31 '17 at 10:13
1

You can also simulate slow network with last versions of Firefox Development Edition. But I'm not sure how much you can control the latency. See this article for more information about this feature.

woshilapin
  • 239
  • 4
  • 19
1

Thanks to @hanshenrik I managed to combine his answer with my own research.

To get a 408 (or any other response) for testing and handling purposes using curl, I made a PHP file containing http_response_code(408); then from my index.php file, I initiated curl:

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "timeout.php");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($curl);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

echo $httpcode;

The important line is the curl_get_info section that will return the http response code from the previous page.I can then handle this code appropriately in my index.php file.

I don't really know if this is the most efficient way to do this, so if anyone can improve my answer, please feel free.

In the meantime, I hope this helps someone else out there!

Jimmyb_1991
  • 346
  • 5
  • 12
0

Not sure about D7 specifically, but I know that you can GuzzleHttp to try-catch RequestException's which includes 400 and 500 series response codes. You can additionally set a timeout, so that if it doesn't respond within X seconds, then just continue. You can also supply the CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT options to your cURL object, as per https://stackoverflow.com/a/11066378/7118098

Community
  • 1
  • 1
Zach
  • 517
  • 2
  • 8