when connection times out using curl or get_file_contents to an API it kills my script

Stephen Funk

When the connection timesout when using curl or get_file_contents because of a network error or remote server doesn't respond for some reason it kills my script.

I am making these remote calls in a loop and if one fails it kills my script.

What is the best way to handle if a specific post fails, that it goes on to the next in the loop instead of the script dying?

ferdynator

First set a parameter for CURL for a timeout limit:

 curl_setopt($ch, CURLOPT_TIMEOUT, 1800);

The result of your curl_exec() call will show you if the request was successful or not:

for(/* anything */) {
    $ch = curl_init(); 
    //...
    $result = curl_exec($ch);
    if (!$result) {
        continue; // use this to jump to the next loop
    }

    // this code will not be executed if the request failed

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Curl request to same server intermittently times out

From Dev

Protractor times out waiting for sync with page when using $resource

From Dev

Appfog file_get_contents and curl connection timeout

From Dev

Can't Submit App to App Store: "Sending API Usage to iTunes Connect" either times out or loses connection

From Dev

Running a node.js server on my VPS on port 3000 and the connection times out

From Dev

Can not get MailChimp to save my data using the API with Curl

From Dev

PHP curl script not closing connection

From Dev

ajax GET request times out for URL when browser and CURL work

From Dev

connection to tdsool times out with tsql

From Dev

What happens to a Postgres row lock when a connection times out?

From Dev

Getting connection time out when I run `curl` command on Google Compute Engine

From Dev

Connection to SSH server times out

From Dev

file_get_contents Connection Timed Out for some contents

From Dev

Protractor times out waiting for sync with page when using $resource

From Dev

Appfog file_get_contents and curl connection timeout

From Dev

keep getting kicked out my google login when using chrome.identity api

From Dev

PHP curl script not closing connection

From Dev

FileZilla times out when transferring large file

From Dev

First Ping/Connection times out

From Dev

Wordpress PHP commented out when using file_get_contents

From Dev

ajax GET request times out for URL when browser and CURL work

From Dev

TFS Deployment times out, when using an existing Build

From Dev

What could cause my SSH connection to die (Connection reset by [ip]) + kills my background process?

From Dev

AWS Gateway API mapping using VTL times out

From Dev

SSH connection times out when trying to remotely connect to my personal computer from laptop on another network

From Dev

Exit PowerShell script using 'exit' kills my program completely

From Dev

Why is using cURL giving me a 403 forbidden error when file_get_contents works?

From Dev

Request times out, cURL does not

From Dev

Is my preg_match, or get_file_contents slowing my page?

Related Related

  1. 1

    Curl request to same server intermittently times out

  2. 2

    Protractor times out waiting for sync with page when using $resource

  3. 3

    Appfog file_get_contents and curl connection timeout

  4. 4

    Can't Submit App to App Store: "Sending API Usage to iTunes Connect" either times out or loses connection

  5. 5

    Running a node.js server on my VPS on port 3000 and the connection times out

  6. 6

    Can not get MailChimp to save my data using the API with Curl

  7. 7

    PHP curl script not closing connection

  8. 8

    ajax GET request times out for URL when browser and CURL work

  9. 9

    connection to tdsool times out with tsql

  10. 10

    What happens to a Postgres row lock when a connection times out?

  11. 11

    Getting connection time out when I run `curl` command on Google Compute Engine

  12. 12

    Connection to SSH server times out

  13. 13

    file_get_contents Connection Timed Out for some contents

  14. 14

    Protractor times out waiting for sync with page when using $resource

  15. 15

    Appfog file_get_contents and curl connection timeout

  16. 16

    keep getting kicked out my google login when using chrome.identity api

  17. 17

    PHP curl script not closing connection

  18. 18

    FileZilla times out when transferring large file

  19. 19

    First Ping/Connection times out

  20. 20

    Wordpress PHP commented out when using file_get_contents

  21. 21

    ajax GET request times out for URL when browser and CURL work

  22. 22

    TFS Deployment times out, when using an existing Build

  23. 23

    What could cause my SSH connection to die (Connection reset by [ip]) + kills my background process?

  24. 24

    AWS Gateway API mapping using VTL times out

  25. 25

    SSH connection times out when trying to remotely connect to my personal computer from laptop on another network

  26. 26

    Exit PowerShell script using 'exit' kills my program completely

  27. 27

    Why is using cURL giving me a 403 forbidden error when file_get_contents works?

  28. 28

    Request times out, cURL does not

  29. 29

    Is my preg_match, or get_file_contents slowing my page?

HotTag

Archive