woocommerce / wc-api-php

WooCommerce REST API PHP Library
https://packagist.org/packages/automattic/woocommerce
MIT License
528 stars 160 forks source link

Fatal error: Uncaught Automattic\WooCommerce\HttpClient\HttpClientException: cURL Error: Operation timed out after 30009 milliseconds with 0 bytes received #261

Open realrecordzLab opened 4 years ago

realrecordzLab commented 4 years ago

I want to use the REST API to get products and categories on the front-end of a website and make an infinite scroll. I'm havong some trouble with the php client library, I'm on localhost and I'm not able to fetch data. I get this error

Fatal error: Uncaught Automattic\WooCommerce\HttpClient\HttpClientException: cURL Error: Operation timed out after 30009 milliseconds with 0 bytes received

require __DIR__ . '/vendor/autoload.php';

  use Automattic\WooCommerce\Client;

  $woocommerce = new Client(
      'http://localhost/ladykent/', // Your store URL
      'ck_32...', // Your consumer key
      'cs_0a...', // Your consumer secret
      [
          'wp_api' => true, // Enable the WP REST API integration
          'version' => 'wc/v3', // WooCommerce WP REST API version
          'timeout'=> 30,
          'verify_ssl'=> false,
      ]
  );

If I remove the ssl verification and the timeout, after setting a custom rest route on wordpress I will get instead this error:

add_action( 'rest_api_init', function(){
    register_rest_route('shop/v1', '/categories',
      array(
        'methods' => WP_REST_Server::READABLE,
        'callback' => 'get_shop_categories'
      )
    );
  });

  function get_shop_categories( $woocommerce ){
    $data = $woocommerce->get('products/categories');
    $response = new WP_REST_Response( $data, 200 );
    return $response;
  }

Fatal error: Uncaught Error: Call to undefined method WP_REST_Request::get()

Is possible to fix this?

maulikmakwana commented 4 years ago

Hi, you can check automattic\woocommerce\src\WooCommerce\HttpClient\HttpClient.php here CURLOPT_TIMEOUT set 0 so its infinite loop set

realrecordzLab commented 4 years ago

Hi, you can check automattic\woocommerce\src\WooCommerce\HttpClient\HttpClient.php here CURLOPT_TIMEOUT set 0 so its infinite loop set

How I can fix this? I've tried also with the rest api, but the auth not work, I think because I've unset the users wp api endpoint?

mkalina commented 4 years ago

Same issue here on Nginx as well as Apache. Following the WooCommerce REST API Docs to the letter with $woocommere->get(''), I get

`Fatal error: Uncaught Automattic\WooCommerce\HttpClient\HttpClientException: cURL Error: Operation timed out after 2000 milliseconds with 0 bytes received in /automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php:417 Stack trace:

0 /vendor/automattic/woocommerce/src/WooCommerce/Client.php(82): Automattic\WooCommerce\HttpClient\HttpClient->request()

1 myplugin-import.php(34): Automattic\WooCommerce\Client->get()

2 /wp-settings.php(377): include_once('/sites/dev-impo...')

3 /wp-config.php(83): require_once('/sites/dev-impo...')

4 /wp-load.php(37): require_once('/sites/dev-impo...')

5 /wp-blog-header.php(13): require_once('/ in /vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php on line 417

    `
t-prod commented 4 years ago

Hi,

Same problem since yesterday. Timeout with this error :

image Do you have any fix ?

kirteeu commented 4 years ago

Any update here ? Facing same issue image

realrecordzLab commented 4 years ago

I think it's a bug of woocommerce and for now no solution is provided by automattic

Il giorno mer 15 lug 2020 alle ore 15:02 kirteeu notifications@github.com ha scritto:

Any update here ? Facing same issue [image: image] https://user-images.githubusercontent.com/67957370/87547947-744ab280-c6c9-11ea-8b5f-2b01e91cd6ca.png

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/woocommerce/wc-api-php/issues/261#issuecomment-658753551, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMKZMUWCQYVT6ED45F6KO63R3WSFXANCNFSM4KZRC7AA .

blift commented 4 years ago

Same issue

Automattic\WooCommerce\HttpClient\HttpClientException cURL Error: Could not resolve host: domain.com

EDIT This is some issue with hosts on local machines. If wordpress site is running on local by flywheel try to enable live link, next try retrieve API - on my side works. (Remember to change url in client object to generated by flywheel xxx.ngrok.io)

joelwalls commented 4 years ago

I fixed this error setting the client timeout to 0. Default is 15. Does anyone know if this is a good practice?

evokelektrique commented 3 years ago

Same here, Any solutions so far ?

ChristianGodoyInformatico commented 3 years ago

tiene un error por exceso del tiempo de ejecución, tiene varias soluciones. 1.- Agregar esta linea de comando el archivo .htaccess de su pagina web: php_value max_execution_time 300 2.- Agregar esta linea al principio de su archivo .php que ejecuta la funcion que estan realizando set_time_limit(300);

luistimana commented 3 years ago

Solo se debe añadir un timeout:

ejm:

$woocommerce = new Client( $url_API_woo, $ck_API_woo, $cs_API_woo, [
'wp_api' => true, 'version' => 'wc/v3', 'timeout' => 400 ] );

ericpereira commented 3 years ago

Solo se debe añadir un timeout:

ejm:

$woocommerce = new Client( $url_API_woo, $ck_API_woo, $cs_API_woo, [ 'wp_api' => true, 'version' => 'wc/v3', 'timeout' => 400 ] );

It's works for me

batistajon commented 3 years ago

I had trouble with config:cache, when it is run, the app Laravel 8 doesn't read files from the .env-file. I ran config:clear, and everything works again.

raylinanthony commented 3 years ago

The problem with me was the All In One Wp Security, I had a 6g Firewall... after disable that, all will works again. Also, try to have a clean .htaccess with any others rules.

atdheboshnjaku commented 2 years ago

Hello guys,

By any chance, does anyone have a concrete solution to this error, I am still getting it even though I have tried all the suggestions mentioned above. However the error changes when applying the timeout => 400 from error on line 417 to : automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php on line 350

I am trying to use the woocommerce API to build a plugin, and I am working on my localhost using MAMP, any idea how to get around this?

bvsk279 commented 1 year ago

Hi,

The authentication of woocommerce rest-api is annoying and I've been getting the same error for a million times until I decided to skip the authentication step.

Just include the following code to bypass the authentication step add_filter( 'woocommerce_rest_check_permissions', 'myfunction_allow_rest_api_queries', 10, 4 ); function myfunction_allow_rest_api_queries( $permission, $context, $zero, $object ) { return true; // Allow all queries. }

Thanks to the following resource https://www.damiencarbery.com/2019/07/disable-woocommerce-rest-api-authentication/

umairtipu commented 1 year ago

Hi everyone,

I was also having the same issue, there were about 300 to 400 products in my Yii project and needs to be exported to wordpress store. I was also facing the same error and I could fix that error by increasing timout amount in settings array like

$woocommerce = new Client(
      'http://localhost/ladykent/', // Your store URL
      'ck_32...', // Your consumer key
      'cs_0a...', // Your consumer secret
      [
          'wp_api' => true, // Enable the WP REST API integration
          'version' => 'wc/v3', // WooCommerce WP REST API version
          'timeout'=> 40000, // it needs to be increased according to your need I increase it to 40000
          'verify_ssl'=> false,
      ]
  );