Invision Community 4: SEO, prepare for v5 and dormant account notifications By Matt Monday at 02:04 PM
JR Network Posted May 1, 2017 Posted May 1, 2017 Hi guys, I'm developing a new application for my board and having an issue with cURL timing out. I'd like to set a check on the code that if it gets a response code other than 200 it switches to another link I know works and continues the foreach loop I have. So for example: $response = \IPS\Http\Url::external( "{$url}{$worlds}" . \mb_strtolower( preg_replace( '/\s+/', '_', $currentWorld ) ) . ".jpg" )->request( 5 )->get(); if ( $response->httpResponseCode == 200 ) { $currentWorldThumb = "{$url}{$worlds}" . \mb_strtolower( preg_replace( '/\s+/', '_', $currentWorld ) ) . ".jpg"; $fallback_cw = true; } else { $currentWorldThumb = \IPS\Theme::i()->resource( 'worlds/world_no_image.jpg', 'serversapp', 'front' ); $fallback_cw = false; } Currently I'm getting the following error: IPS\Http\Request\CurlException: Operation timed out after 1003 milliseconds with 0 bytes received (28) This is happening because the world isn't there for the script to load the image. It keeps retrying I believe to get response code 200. But this causes the script to slow down and cause a minute load times and give the error. Is there anything I'm missing that I can add to make the script say okay, no image there for that world, continue on and use this link for now? Can anyone help me out on my predicament. Thanks
Daniel F Posted May 1, 2017 Posted May 1, 2017 I would suggest to put your code into a try-catch block. try { $response = \IPS\Http\Url::external( "{$url}{$worlds}" . \mb_strtolower( preg_replace( '/\s+/', '_', $currentWorld ) ) . ".jpg" )->request( 5 )->get(); if ( $response->httpResponseCode == 200 ) { ....... } } catch ( \IPS\Http\Request\Exception $e ) { $currentWorldThumb = \IPS\Theme::i()->resource( 'worlds/world_no_image.jpg', 'serversapp', 'front' ); $fallback_cw = false; }
JR Network Posted May 1, 2017 Author Posted May 1, 2017 20 minutes ago, Daniel F said: I would suggest to put your code into a try-catch block. try { $response = \IPS\Http\Url::external( "{$url}{$worlds}" . \mb_strtolower( preg_replace( '/\s+/', '_', $currentWorld ) ) . ".jpg" )->request( 5 )->get(); if ( $response->httpResponseCode == 200 ) {....... } catch ( \IPS\Http\Request\Exception $e ) { $currentWorldThumb = \IPS\Theme::i()->resource( 'worlds/world_no_image.jpg', 'serversapp', 'front' ); $fallback_cw = false; } Am I removing the else in favour for the catch? If I'm correct. Thanks for your quick response.
Daniel F Posted May 1, 2017 Posted May 1, 2017 It's really depending on the response from your request.. I would probably still verify the responsecode and probably even the type to make sure it's an image ( if it should be always an image )
Recommended Posts
Archived
This topic is now archived and is closed to further replies.