googleapis / google-api-php-client

A PHP client library for accessing Google APIs
http://googleapis.github.io/google-api-php-client/
Apache License 2.0
9.34k stars 3.52k forks source link

simple-query.php example returns 'HTTP Error: Unable to connect: '0'' #196

Closed rowanrh closed 10 years ago

rowanrh commented 10 years ago

Hi -

I am newish to PHP and very new to the Google API's, so my apologies in advance if I am making an idiot of myself.

As mentioned, I am new to the Google API's, so after downloading the source I decided to start with the simple-query example.

I created a Simple API key - with no restrictions on the URI's - inserted it in the code, ran the code and promptly received the above error message, i.e. "HTTP Error: Unable to connect: '0'".

After some playing I was able to identify the problem line as being line #117 in the module IO\Abstract, i.e.:

"list($responseData, $responseHeaders, $respHttpCode) = $this->executeRequest($request);"

A var_dump of $request returned the following: class Google_Http_Request#21 (14) { private $batchHeaders => array(3) { 'Content-Type' => string(16) "application/http" 'Content-Transfer-Encoding' => string(6) "binary" 'MIME-Version' => string(3) "1.0" } protected $queryParams => array(3) { 'q' => string(19) "Henry David Thoreau" 'filter' => string(11) "free-ebooks" 'key' => string(39) "**_yCo0_****Jvima****" } protected $requestMethod => string(3) "GET" protected $requestHeaders => array(0) { } protected $baseComponent => string(26) "https://www.googleapis.com" protected $path => string(17) "/books/v1/volumes" protected $postBody => NULL protected $userAgent => string(56) "Client_Library_Examples google-api-php-client/1.0.5-beta" protected $canGzip => NULL protected $responseHttpCode => NULL protected $responseHeaders => NULL protected $responseBody => NULL protected $expectedClass => string(28) "Google_Service_Books_Volumes" public $accessKey => NULL }

A try-catch block around the offending line returned the following: I/O Abstract call error: HTTP Error: Unable to connect: '0'

0 C:\srv\GoogleApi\Google\IO\Abstract.php(120): Google_IO_Stream->executeRequest(Object(Google_Http_Request))

1 C:\srv\GoogleApi\Google\Http\REST.php(42): Google_IO_Abstract->makeRequest(Object(Google_Http_Request))

2 C:\srv\GoogleApi\Google\Client.php(499): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request))

3 C:\srv\GoogleApi\Google\Service\Resource.php(195): Google_Client->execute(Object(Google_Http_Request))

4 C:\srv\GoogleApi\Google\Service\Books.php(2304): Google_Service_Resource->call('list', Array, 'GoogleService...')

5 C:\srv\GoogleApi\simple-query.php(68): Google_Service_Books_Volumes_Resource->listVolumes('Henry David Tho...', Array)

6 {main}

NB: The line number shown above is 120, i.e. original line number #117 plus three lines for the var_dump and the try-catch.

Technical stuff: Windows 7 Ultimate / PHP 5.5.11 / Apache 2.4

Tested using PhpStorm version 7 / IE version 11 / Firefox version 29

Any assistance greatly appreciated ...

ianbarber commented 10 years ago

Can you see if the call works if you make it manually, with cURL etc or in the browser, by visiting:

https://www.googleapis.com/books/v1/volumes?q=Henry David Thoreau&filter=free-ebooks&key=

One thing I discovered fairly recently is there are some countries that aren't allowed to call the books API, so I'm wondering if its something along those lines?

rowanrh commented 10 years ago

Both IE & FireFox failed on the query as written (i.e. '&key=') with HTTP 400 errors.

However, when I dropped the '&key=' (i.e. so the query ended '&filter=free-ebooks') the query worked, IE with a file open/save prompt for 'volumes.json', FireFox with data, i.e. the json opened.

I must say that I have never heard of Google blocking, but then Google have a local office and doing quite well business-wise :-)

ianbarber commented 10 years ago

Ah OK, sounds like the key is the problem if it works without then: can you check that the Books API is enabled in the developer console for the project associated with that key?

rowanrh commented 10 years ago

I deleted the existing key, then generated a new API key for the project.

The code failed with the same error message, as did a blank key.

I then checked the key by calling manually (as per your original request) using the newly-generated API key. The calls worked in both IE & FireFox.

My PC experience being mainly in Windows LOB applications I know very little about PHP & Apache.

That said, the application appears to fail when PHP or Apache (i,e. localhost / 127.0.0.1) are involved.

Is it possible that the problem might lie there ?

.......................................

Later. The Apache error log reports the following:

[Mon May 19 09:51:42.575700 2014] [mpm_winnt:warn] pid 1592:tid 804The specified network name is no longer available. : AH00341: winnt_accept: Asynchronous AcceptEx failed.

This appears to be as the result of a long-outstanding bug in Apache. I have downloaded the latest version of Apache - 2.4.9 vs. the 2.4.6 that I am running - and will install it tomorrow.

Will let you know of the results ...

On Mon, May 19, 2014 at 9:01 PM, Ian Barber notifications@github.comwrote:

Ah OK, sounds like the key is the problem if it works without then: can you check that the Books API is enabled in the developer console for the project associated with that key?

— Reply to this email directly or view it on GitHubhttps://github.com/google/google-api-php-client/issues/196#issuecomment-43542774 .

ianbarber commented 10 years ago

Yeah, sorry, I wasn't questioning the key, but whether the API was enabled in the APIs section of the project. If you go to the developer console, in the same menu as credentials there should be an APIs menu - can you check Books is enabled in that list? Being that you have the same issue when making the call directly, I suspect the apache issues are unrelated.

rowanrh commented 10 years ago

My apologies: I abbreviated the reply.

The Books API is enabled in the console. That said, I created the initial Simple API key before enabling the Books API, so - just in case - I deleted the first key then created a new Simple API key i.e. after the Books API was enabled. According to the instructions the first key should have worked, but hey, we are talking about computers - and programmers - here! :)

I have tested with a blank key and two generated Simple API keys. The results (thus far) are that a manual call always works. whereas a programmatic call never works,

Many thanks for your assistance thus far.

On Mon, May 19, 2014 at 11:16 PM, Ian Barber notifications@github.comwrote:

Yeah, sorry, I wasn't questioning the key, but whether the API was enabled in the APIs section of the project. If you go to the developer console, in the same menu as credentials there should be an APIs menu - can you check Books is enabled in that list? Being that you have the same issue when making the call directly, I suspect the apache issues are unrelated.

— Reply to this email directly or view it on GitHubhttps://github.com/google/google-api-php-client/issues/196#issuecomment-43559374 .

LindaLawton commented 10 years ago

There is someone else with this error message on StackOverflow. His is related to plus.login. I think he is doing it wrong, but that's beside the point. The point is they are both seeing this Unable to Connect: '0' error message, which makes me wonder. So i thought i would bring it to your attention. http://stackoverflow.com/q/23742576/1841839

rowanrh commented 10 years ago

Thanks for the heads-up. I also reported my error (i.e. this one) but unfortunately no responses.

What is interesting is that the code is completely different yet the failure is the same ... 8(

On Tue, May 20, 2014 at 11:13 AM, Linda Lawton notifications@github.comwrote:

There is someone else with this error message on StackOverflow. His is related to plus.login. I think he is doing it wrong, but that's beside the point. The point is they are both seeing this Unable to Connect: '0' error message, which makes me wonder. So i thought i would bring it to your attention. http://stackoverflow.com/q/23742576/1841839

— Reply to this email directly or view it on GitHubhttps://github.com/google/google-api-php-client/issues/196#issuecomment-43602897 .

LindaLawton commented 10 years ago

Just as an off chance that its this. When you created your app in Developers console did you remember to go to Apis & auths -> consent Screen and add an email Address and Product name? Both those values must be set or you get an error from the client. The thing is its a different error message in .net client lib (think its invalid client_id), but i wonder if maybe the PHP client lib just isn't spiting out the same error message. I can't test this until I get home tonight, was just an idea.

WaldoJeffers commented 10 years ago

@LindaLawton Hey ! I'm the "someone else who is doing it wrong" (no offense taken, I'm new to google php client library and I am trying to follow different tutorials regarding the subject). Please could you tell me what I am doing wrong so I could update my code, and maybe solve the problem ?

I also updated my post on StackOverflow in the comments. I am not sure at all but I think the Apache configuration has something to do with it. When I uploaded the same files on an online "regular" server, this strange error did not show up. (Instead, I get Error fetching OAuth2 access token, message: 'invalid_grant').

What is very weird is that the code obtained when running the tests on WAMPServer (localhost) is actually valid (it works when used with a cURL request, I get a refresh_token) but the code obtained when running the tests online is not ! I get "invalid grant, invalid code" error.

I use the same client_id & client_secret in both cases.

I can not understand it. My javascript origin is correct (when I try to change a single letter, I instantly get an origin_mismatch error when clicking on the sign-in button).

I don't know if it can help you; I'm willing to run any tests that come to your mind.

As for your suggestion Linda, in my case, email Adress and Product are correctly set, and displayed when the app asks my permission.

EDIT : Regarding my issue local vs server, maybe it is linked to this topic on StackOverflow

rowanrh commented 10 years ago

Hi Linda -

After upgrading to Apache 2.4.9 I went to the console, added the email address & product name, saved then tried again. No luck: the same error message was returned.

The Apache log is alos still returning he same error, i.e.: [Tue May 20 14:44:25.180600 2014] [mpm_winnt:warn] [pid 8104:tid 836](OS 64)The specified network name is no longer available. : AH00341: winnt_accept: Asynchronous AcceptEx failed.

On Tue, May 20, 2014 at 11:55 AM, Linda Lawton notifications@github.comwrote:

Just as an off chance that its this. When you created your app in Developers console did you remember to go to Apis & auths -> consent Screen and add an email Address and Product name? Both those values must be set or you get an error from the client. The thing is its a different error message in .net client lib (think its invalid client_id), but i wonder if maybe the PHP client lib just isn't spiting out the same error message.

— Reply to this email directly or view it on GitHubhttps://github.com/google/google-api-php-client/issues/196#issuecomment-43606602 .

LindaLawton commented 10 years ago

@rowanrh i tested the simple-query.php example and it works fine. I'm wondering it has something to do with your connection, your app set up in Google Developer Console, or even that you didn't copy the full Google Directory.

So here is an insane test. The following zip is a working example using my API key.

  1. If it doesn't work then its probably your connection.
  2. If it does work then its probably your app key or the directory a. try and swap it for your app key. If it doesn't work we know its your app key that's bugged. If it does then your all set, and can start having fun with the APIs. :)

If it does work with my app key but not yours we need to fix yours. Its a development App key, I delete it from time to time, because i tend to leak it out on the net by mistake, or like now to help people test issues.

http://www.daimto.com/Tutorials/PHP/Books.zip

@WaldoJeffers looking into yours now. I'm still trying to recreate the bug.

rowanrh commented 10 years ago

Hi Linda -

Let me start by thanking you for your efforts - you have no idea how much they are appreciated.

I unzipped your file and copied the results to the Apache document root, i.e. "c:/etc/Apache24/htdocs"

I then started "localhost" in IE and selected the "Books" folder. IE returned a HTTP 500. Firefox did the same.

I then ran the query manually. It worked 100%.

The Apache error log was clean, whilst the PHP error log reported as before, i.e.

[20-May-2014 16:57:00 UTC] PHP Fatal error: Uncaught exception 'Google_IO_Exception' with message 'HTTP Error: Unable to connect: '0'' in C:\etc\Apache24\htdocs\Books\Google\IO\Stream.php:118 Stack trace:

0 C:\etc\Apache24\htdocs\Books\Google\IO\Abstract.php(122):

Google_IO_Stream->executeRequest(Object(Google_Http_Request))

1 C:\etc\Apache24\htdocs\Books\Google\Http\REST.php(42):

Google_IO_Abstract->makeRequest(Object(Google_Http_Request))

2 C:\etc\Apache24\htdocs\Books\Google\Client.php(499):

Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request))

3 C:\etc\Apache24\htdocs\Books\Google\Service\Resource.php(195):

Google_Client->execute(Object(Google_Http_Request))

4 C:\etc\Apache24\htdocs\Books\Google\Service\Books.php(2304):

Google_Service_Resource->call('list', Array, 'GoogleService...')

5 C:\etc\Apache24\htdocs\Books\index.php(58):

Google_Service_Books_Volumes_Resource->listVolumes('Henry David Tho...', Array)

6 {main}

thrown in C:\etc\Apache24\htdocs\Books\Google\IO\Stream.php on line 118

I agree that it must be a connection problem. Is there anything in Apache httpd.conf or httpd-vhosts.conf that requires setting ? Or PHP ?

On Tue, May 20, 2014 at 5:14 PM, Linda Lawton notifications@github.comwrote:

@rowanrh https://github.com/rowanrh i tested the simple-query.php example and it works fine. I'm wondering it has something to do with your connection, your app set up in Google Developer Console, or even that you didn't copy the full Google Directory.

So here is an insane test. The following zip is a working example using my API key.

  1. If it doesn't work then its probably your connection.
  2. If it does work then its probably your app key or the directory a. try and swap it for your app key. If it doesn't work we know its your app key that's bugged. If it does then your all set, and can start having fun with the APIs. :)

If it does work with my app key but not yours we need to fix yours. Its a development App key, delete from time to time it because i tend to leak it out on the net by mistake, or like now to help people test issues.

http://www.daimto.com/Tutorials/PHP/Books.zip

@WaldoJeffers https://github.com/WaldoJeffers looking into yours now. I'm still trying to recreate the bug.

— Reply to this email directly or view it on GitHubhttps://github.com/google/google-api-php-client/issues/196#issuecomment-43639475 .

LindaLawton commented 10 years ago

You should just be able to do http://localhost/books/

Last idea, I was also unable to get Apache on windows to work with the client lib when i tried a month ago. I wondered if it had to do with the fact that all the includes where using the Linux Dir (/) structure and Apache on windows well looks for the windows Dir structure () Someone recommended i try XAMPP and it worked like a charm https://www.apachefriends.org/index.html . If that doesn't work i'm out of ideas.

btw where are you in the world?

rowanrh commented 10 years ago

Hi Linda -

http://localhost/books/ doesn't work for me

Windows is in fact capable of using both '\' and '/', though I will agree that sometimes it gets a tad flustered :)

That said, I ran the code through PhpStorm and it happily resolved all file references.

I have now asked the members of Apache Lounge for assistance, and will advise of any news.

Once more, thanks for the assistance.

On Tue, May 20, 2014 at 7:40 PM, Linda Lawton notifications@github.comwrote:

You should just be able to do http://localhost/books/

Last idea, I was also unable to get Apache on windows to work with the client lib when i tried a month ago. I wondered if it had to do with the fact that all the includes where using the Linux Dir (/) structure and Apache on windows well looks for the windows Dir structure () Someone recommended i try XAMPP and it worked like a charm https://www.apachefriends.org/index.html . If that doesn't work i'm out of ideas.

— Reply to this email directly or view it on GitHubhttps://github.com/google/google-api-php-client/issues/196#issuecomment-43658934 .

rowanrh commented 10 years ago

Hi Linda -

Apologies, I missed your question. I am in South Africa.

rowanrh commented 10 years ago

Hi -

I finally resolved the issue by changing the following PHP.ini settings:

  1. I set (by uncommenting) the extension=php_openssl.dll - necessary for 'https' URLs; and
  2. I set the time-zone. Google is fussy about times, so this might have affected the outcome.

Thanks to all for the assistance.

WaldoJeffers commented 10 years ago

@rowanrh : It did indeed solve the problem for me too. Thank you very much sir !

rowanrh commented 10 years ago

@Waldo: My pleasure - I'm glad that I could assist.

MarcAlfa commented 10 years ago

Hi, i had the same problem with wamp. I resolved it just enabling the php_curl extension in wamp. That's works for me

alexylem commented 6 years ago

I had the same error with nginx/php-fpm which got fixed by installing & enabling curl php extension.