bigcommerce / bigcommerce-api-php

Connect PHP applications with the Bigcommerce Platform
https://developer.bigcommerce.com
MIT License
144 stars 185 forks source link

Empty reply from server after updating brand followed by immediately creating product #138

Open flyingL123 opened 9 years ago

flyingL123 commented 9 years ago

I am attempting to first update a brand, then create a product using the Bigcommerce API. I am doing so with the following code:

Bigcommerce::updateBrand(48, ['name'=>'Del Sol']);
echo "Brand Updated";
Bigcommerce::createProduct(['name'=>'New Test'.rand(), 'weight'=>3, 'price'=>3, 'availability'=>'available', 'categories'=>[1582], 'type'=>'physical']);
echo "Product Created";

This results in an Bigcommerce\Api\NetworkError being returned, with the message Empty reply from server.

If I run either of the commands above by themselves, the request succeeds. If I switch the order of the commands, so that the product is created and then the brand is updated, both requests succeed. However, when I run them as shown, the updateBrand command works correctly, but the createProduct command hangs for a while and then throws the Bigcommerce\Api\NetworkError.

Why would this be happening?

aleachjr commented 9 years ago

@flyingL123 can you provide me the store domain that you are noticing issues with?

flyingL123 commented 9 years ago

The domain is http://mikedev.mybigcommerce.com/. Store hash is grbzj9js. Please let me know if you need anything else.

aleachjr commented 9 years ago

This looks like a test site, any reason I cannot grab the api credentials from admin and run a few tests directly to the site?

flyingL123 commented 9 years ago

That's totally fine. You can do whatever you need.

aleachjr commented 9 years ago

I am not able to reproduce, see the attached screenshot as well as code that I used to produce it.

screen shot 2015-05-05 at 8 26 35 am screen shot 2015-05-05 at 8 25 50 am

flyingL123 commented 9 years ago

Could it make a difference that I am using oAuth and you aren't?

aleachjr commented 9 years ago

It should not matter. I do see the error that you had in the logs, it is reported as 409 Resource Conflict. Can you try again?

aleachjr commented 9 years ago

Also, out of curiosity, how are you using the PHP client with oAuth? We have not released this functionality in this library.

flyingL123 commented 9 years ago

I just made some minor changes to the client to support oAuth: https://github.com/flyingL123/bigcommerce-api-php

I'm receiving this error consistently 100% of the time. The only difference between our code is that I'm using oAuth. Are you sure this wouldn't cause the problem? Could I provide you with the oAuth credentials so you could try it that way?

Also, are you sure that 409 resource conflict is a result of this? Normally a resource conflict gets returned as a client error, not the network error like I'm seeing here. I had received resource conflict errors earlier today for other reasons, so I just want to make sure you aren't seeing that and assuming it's from this code.

flyingL123 commented 9 years ago

I just tried again a few more times, and received the same error.

flyingL123 commented 9 years ago

Hi @aleachjr - I just tried the same code using standard Auth instead of oAuth, and it worked. Any idea why this is happening?

aleachjr commented 9 years ago

Upon looking at your branch, I also notice that you are using code that is 40 commits behind current master. I would suggest that you update your branch to the latest master code and try again. If that continues to fail, try this without using oAuth as it is not currently supported in the library. I am unable to test this using unsupported code at this time.

flyingL123 commented 9 years ago

I understand. The problem is that Bigcommerce requires oAuth for certain things, like webhooks, but then your SDK's don't support oAuth, so it puts us developers in a tough position. Are you at least able to see if there are any errors occurring on your server that would cause this empty response?

I understand that you can't just take my word for it, but there is nothing in my branch that would be causing this. It works in every other scenario as expected. It even works fine if you just change the order of the the two commands we're discussing.

Thank you for your help.

aleachjr commented 9 years ago

I apologize for the hassle. We are aiming to have an oAuth supported library released within the next 30 days. Additionally, upon looking at server logs, I am not seeing any errors to your store since the last one about an hour ago. If you email me directly (anthony.leach@bigommerce.com) we can talk about the changes you made to the library to support oAuth.

sudosoul commented 9 years ago

Hey @flyingL123 just wanted to chime in that I tested your code with the newest library (with my own oAuth support), and it worked successfully. Can you make sure that product is adhering to the limits here? https://developer.bigcommerce.com/blog/changelog/api-limits

Also, are you positive you are not being rate limited? Seems the rate limiting on oAuth is somewhat random. I have a version of the Client that will automatically sleep, and repeat the the request upon being rate limited, if that helps I can upload that here today. https://github.com/bigcommerce/bigcommerce-api-php/pull/132 (latest with oAuth)

flyingL123 commented 9 years ago

Thanks @mullinsr . My product is adhering to the limits in the link you provided. If I make the createProduct call on its own, not immediately following an updateBrand call, it works correctly, so the product itself is fine.

As far as I know, if I were being rate limited, I would be receiving a 429 Too Many Requests response from the server. Is that correct? In this case, I am receiving a curl error, as if the server is completely unavailable, so I do not believe I'm being rate limited. Is there somewhere I can see your version of the newest library using oAuth?

sudosoul commented 9 years ago

Hi @flyingL123

Yes you would receive 429 but it would not be available from Bigcommerce::getLastError() (at least not in my experience), so you would have to manually parse the body or view the headers. There have been a decent amount of changes done to this library, thanks to @aleachjr and the team, and so using the updated version would be highly recommended. You can view my version here, it uses this Bigcommerce library from about a month ago. https://github.com/mullinsr/bigcommerce-api-php Note this version moderately modifies Connection.php, so that it will automatically sleep and repeat the request upon being rate limited, but I hope you find this useful as you do not have to include rate limit handling from within your app logic itself ;)

You authenticate through oAuth using:

Bigcommerce::configure($user, true);
//Where $user includes:
$user = array(
  'oauth_token' => 'xx',
  'store_hash'   => 'xx,
  'client_id'       => 'xx'
);

We use true in the Bigcommerce::configure() call to signify that we wish to use oAuth. For legacy api authentication, simply remit the true, and connect as you would normally.

Let me know if using the updated library solves your issue, Rob

flyingL123 commented 9 years ago

Thanks for the info. I actually have failOnError set to true, so if it was a rate limiting issue, I would see a ClientError being thrown, which is not the case. I love the automatic retry feature. I hope that gets incorporated into the main library. Thanks again.

shprabin commented 6 years ago

Hi, i am having same kind of issue. Were you able to fix this issue? If i POST a customfields and GET a product right after posting. Empty product is returned. But if i close and reopen the connection which will init_curl() between two of these calls it works fine. This looks like something is not reseting between two curl calls. I am testing in xamp in windows. I could not figure out whats going on in here.

sudosoul commented 6 years ago

@shprabin - the issue that was happening for me was that i was receiving empty responses because the requests were actually failing silently with '429 - too many requests' ... this was confirmed by me printing the full response data (headers, statusCode, etc).

so, to see if maybe you are experiencing this same issue, i would ask you to print the full response data after you make that GET request to see if maybe you are being 429 rate limited.

EDIT: or, just implement a delay between the two requests.

HTH