Jump to content

cURL Timeout


JR Network

Recommended Posts

Posted

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

Posted

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;
}

 

Posted
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.

Posted

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 )

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...