Cannot connect to GSA API using file_get_contents in PHP

6 views
Skip to first unread message

Danny Siu

unread,
Jan 15, 2015, 4:55:47 AM1/15/15
to Google-Search-...@googlegroups.com
I have written a php program to try to connect to GSA API:

$data = array('Email'=>$Email, 'Passwd'=>$Passwd);

$opts = array('http' =>
array(
'method'  => 'POST',
'header'  => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);

$context  = stream_context_create($opts);
$result = file_get_contents($auth_url, false, $context);
var_dump($result);

And it is not working, it gives me failed to open stream: Connection timed out

so I tried using curl:

$data = array('Email'=>$Email, 'Passwd'=>$Passwd);

$ch = curl_init();

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_URL, $auth_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec ($ch);
curl_close ($ch);

var_dump($server_output);

And this returns to me bool(false)

How can I connect to the GSA API?

Mathias Bierl

unread,
Jan 15, 2015, 4:57:07 AM1/15/15
to Google-Search-...@googlegroups.com
How does your $auth_url looks like ?

Danny Siu

unread,
Jan 15, 2015, 5:02:24 AM1/15/15
to Google-Search-...@googlegroups.com

Mathias Bierl

unread,
Jan 15, 2015, 5:53:43 AM1/15/15
to Google-Search-...@googlegroups.com
Are you sure that the connection can be opened, so firewall let it pass through from you PHP server ?
Can you do a telnet from there ?

Dave Watts

unread,
Jan 15, 2015, 1:31:17 PM1/15/15
to Google-Search-...@googlegroups.com
Whenever you use a client program to connect to an HTTPS URL, the
client will need to either include the server's certificate chain in
its keystore or not verify certificates in the first place. For cURL,
that's done like this:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

For your PHP program, if it's using cURL I guess you'd do the same
thing. If it's using something else, you'll have to figure out how to
either install certificates or not verify them.

Dave Watts, CTO, Fig Leaf Software
1-202-527-9569
http://www.figleaf.com/
http://training.figleaf.com/

Fig Leaf Software is a Service-Disabled Veteran-Owned Small Business
(SDVOSB) on GSA Schedule, and provides the highest caliber vendor-
authorized instruction at our training centers, online, or onsite.

Dave Watts

unread,
Jan 15, 2015, 1:32:26 PM1/15/15
to Google-Search-...@googlegroups.com
> Can you do a telnet from there ?

I don't think telnet can really be used to test HTTPS connections.
You'd have to use openssl s_client or something like that.

Mathias Bierl

unread,
Jan 15, 2015, 2:32:25 PM1/15/15
to Google-Search-...@googlegroups.com
You can use telnet to see if the network connection itself is working.
It is only difficult with telnet to send a real request

Danny Siu

unread,
Jan 15, 2015, 8:27:44 PM1/15/15
to Google-Search-...@googlegroups.com
I did add curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); but the problem is still there

Dave Watts於 2015年1月16日星期五 UTC+8上午2時31分17秒寫道:

Danny Siu

unread,
Jan 15, 2015, 8:30:57 PM1/15/15
to Google-Search-...@googlegroups.com
I have tried to enter BOTH commands:

telnet gsa1.hkedcity.net 8443

in my terminal, the result is:

connect to address 10.0.1.35: Connection timed out

Mathias Bierl於 2015年1月16日星期五 UTC+8上午3時32分25秒寫道:

Mathias Bierl

unread,
Jan 16, 2015, 2:46:11 AM1/16/15
to Google-Search-...@googlegroups.com
Then you have a network issue which you have to solve first

Danny Siu

unread,
Jan 18, 2015, 8:34:19 PM1/18/15
to Google-Search-...@googlegroups.com
However, if I can access https://gsa1.hkedcity.net:8443 using chrome, is it still a network issue?

Mathias Bierl於 2015年1月16日星期五 UTC+8下午3時46分11秒寫道:

Danny Siu

unread,
Jan 18, 2015, 11:38:22 PM1/18/15
to Google-Search-...@googlegroups.com
Hi Dave Watts,
I typed openssl s_client -connect gsa1.hkedcity.net:8443 and it gives me connect:errno=29

Dave Watts於 2015年1月16日星期五 UTC+8上午2時32分26秒寫道:

Mathias Bierl

unread,
Jan 19, 2015, 3:32:16 AM1/19/15
to Google-Search-...@googlegroups.com
Yes could be. If you access by Browser there could be a proxy server which translates the request so no direct connection is needed.

Best would be to talk to your network colleagues.
Reply all
Reply to author
Forward
0 new messages