How to Solve Ripcord Could Not Access Odoo Error

In my previous article, I have written about how to call the odoo API using the ripcord library in the PHP programming language. But, it turns out that this library has some bugs, especially if the API calling process is too long.

In my case, I have to send a large amount of data to the odoo server, where the data processing took more than 3 minutes to complete. The data is successfully received and processed by odoo, but my PHP code does not get a response from the odoo server, instead, it displays an error as shown below.

Ripcord can not access odoo server

It turned out that this error occurred because the ripcord code had caused a timeout, while the data processing on the odoo server was still running. Ripcord itself does not control how long the timeout will occur. Therefore, to solve this problem we have to change the ripcord_client.php file to set the timeout.

Please take a look at the code starting on line 464 in the ripcord_client.php file as shown below.

/**
 * This method posts the request to the given url.
 * @param string $url The url to post to.
 * @param string $request The request to post.
 * @return string The server response
 * @throws Ripcord_TransportException (ripcord::cannotAccessURL) when the given URL cannot be accessed for any reason.
 */
public function post( $url, $request ) 
{
	$options = array_merge( 
		$this->options, 
		array( 
			'http' => array(
				'method' => "POST",
				'header' => "Content-Type: text/xml",
				'content' => $request
			) 
		) 
	);
	$context = stream_context_create( $options );
	$result  = @file_get_contents( $url, false, $context );
	$this->responseHeaders = $http_response_header;
	if ( !$result )
	{
		throw new Ripcord_TransportException( 'Could not access ' . $url, 
			ripcord::cannotAccessURL );
	}
	return $result;
}

To add a timeout configuration, we can change the code above like this.

/**
 * This method posts the request to the given url.
 * @param string $url The url to post to.
 * @param string $request The request to post.
 * @return string The server response
 * @throws Ripcord_TransportException (ripcord::cannotAccessURL) when the given URL cannot be accessed for any reason.
 */
public function post( $url, $request ) 
{
	$options = array_merge( 
		$this->options, 
		array( 
			'http' => array(
				'method' => "POST",
				'header' => "Content-Type: text/xml",
				'content' => $request,
				'timeout' => 3600, // add a timeout to prevent the could not access odoo error
			) 
		) 
	);
	$context = stream_context_create( $options );
	$result  = @file_get_contents( $url, false, $context );
	$this->responseHeaders = $http_response_header;
	if ( !$result )
	{
		throw new Ripcord_TransportException( 'Could not access ' . $url, 
			ripcord::cannotAccessURL );
	}
	return $result;
}

After adding a timeout, can it run smoothly ? Apparently not, the could not access odoo server error like in the picture above is not appear, but it causes another error, now the page is blank, not show anything.

The cause is the code below.

$result  = @file_get_contents( $url, false, $context );

Since the file_get_contents function is preceded by the @ symbol, any error messages caused by the file_get_contents function will not be displayed by PHP. Therefore the result is blank. OK. Now let’s remove the @ symbol from the file_get_contents function, so it looks like below.

/**
 * This method posts the request to the given url.
 * @param string $url The url to post to.
 * @param string $request The request to post.
 * @return string The server response
 * @throws Ripcord_TransportException (ripcord::cannotAccessURL) when the given URL cannot be accessed for any reason.
 */
public function post( $url, $request ) 
{
	$options = array_merge( 
		$this->options, 
		array( 
			'http' => array(
				'method' => "POST",
				'header' => "Content-Type: text/xml",
				'content' => $request,
				'timeout' => 3600, // add a timeout to prevent the could not access odoo error
			) 
		) 
	);
	$context = stream_context_create( $options );
	$result  = file_get_contents( $url, false, $context );
	$this->responseHeaders = $http_response_header;
	if ( !$result )
	{
		throw new Ripcord_TransportException( 'Could not access ' . $url, 
			ripcord::cannotAccessURL );
	}
	return $result;
}

Now the result is no longer blank, but an error appears like in the image below.

Ripcord cause maximum execution time error

It turns out that the process of the file_get_contents function is taking too long time, and the time has exceeded the limit set by PHP. But calm down, we can change the Maximum execution time limit which is only 120 seconds by default with any value that we want. We can change it through configuration in the php.ini file or in the code itself. I prefer to set it in the code, so I can change the code from the ripcord_client.php file above to something like below.

/**
 * This method posts the request to the given url.
 * @param string $url The url to post to.
 * @param string $request The request to post.
 * @return string The server response
 * @throws Ripcord_TransportException (ripcord::cannotAccessURL) when the given URL cannot be accessed for any reason.
 */
public function post( $url, $request ) 
{
	$options = array_merge( 
		$this->options, 
		array( 
			'http' => array(
				'method' => "POST",
				'header' => "Content-Type: text/xml",
				'content' => $request,
				'timeout' => 3600, // add a timeout to prevent the could not access odoo error
			) 
		) 
	);
	set_time_limit(3600); // set a bigger limit, to prevent the maximum execution time error
	$context = stream_context_create( $options );
	$result  = file_get_contents( $url, false, $context );
	$this->responseHeaders = $http_response_header;
	if ( !$result )
	{
		throw new Ripcord_TransportException( 'Could not access ' . $url, 
			ripcord::cannotAccessURL );
	}
	return $result;
}

Now ripcord can send big data and takes a long time to process to odoo server. Remember the timeout and maximum execution time configuration value above are in seconds.

Hopefully, this article is useful for you.

Related Article

Leave a Reply

Your email address will not be published.