What are the best practices for handling errors such as timeouts or network issues when executing PHP scripts on a different server?

When executing PHP scripts on a different server, it's important to handle errors such as timeouts or network issues gracefully. One way to do this is by using try-catch blocks to catch exceptions and handle them appropriately. Additionally, setting appropriate timeout values and implementing retries can help mitigate network-related issues.

try {
    $client = new GuzzleHttp\Client();
    $response = $client->request('GET', 'http://example.com/api/data', ['timeout' => 5]);
    
    // Process the response data
    $data = json_decode($response->getBody(), true);
    
    // Handle the data
    
} catch (GuzzleHttp\Exception\RequestException $e) {
    if ($e->hasResponse()) {
        // Handle HTTP errors
        $response = $e->getResponse();
        echo $response->getStatusCode();
    } else {
        // Handle network issues
        echo "Network error: " . $e->getMessage();
    }
} catch (Exception $e) {
    // Handle other exceptions
    echo "An error occurred: " . $e->getMessage();
}