How to make php https request and read response?
I wan to send some post data to a server and read the response.The url they have provided me is https://thesite.com/page/test.jsp,i tried using $fp = fsockopen("https://thesite.com/page/test.jsp", 80, $errno, $errstr, 30); but got the 'Unable To Fin开发者_Python百科d The "HTTPs"' error.Tried sending data using curl but checked with the server,they received no request.Is there any other way to do it?
I had a simillar problem, and the problem was that curl, by default, does not open websites with untrusted certificates. So you have to disable this option: curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false); So the full script in curl for making a http request would be:
$c = curl_init();
//webpage to which you try to send post
curl_setopt($c, CURLOPT_URL, 'https://www.website/send_request.php');
curl_setopt($c, CURLOPT_POST, true);
// data to be sent via post
curl_setopt($c, CURLOPT_POSTFIELDS, 'var1=324213&var2=432');
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
// Get the response and close the channel.
curl_exec ($c);
// read the error code if there are any errors
if(curl_errno($c))
{
echo 'Curl error: ' . curl_error($c);
}
curl_close ($c);
You're accessing the wrong port. HTTPS is usually reachable on port 443:
$fp = fsockopen('ssl://example.com', 443, $errno, $errstr, 30);
Also, you'll need to specify a socket transport identifier with fsockopen
. In this example, it's ssl://
.
While goreSplatter's answer is correct, there are 3 layered protocols you are dealing with here - HTTP on top of SSL on top of sockets (which in turn run on top of the IP stack). Your approach only addresses one of the 3 (sockets).
goreSplatter's approach still requires you to implement your own HTTP stack to handle communications with the server - this is not a trivial task.
I don't think its possible to POST data using the file wrappers (might be possible using stream wrappers), but I'd suggest you use cURL to access the URL and save yourself a lot of pain.
There are lots of examples you can find on Google - here's one
精彩评论