1

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 Find 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?

Zulu
  • 21
  • 1
  • 4
  • please point out why none of these help solve your question: http://stackoverflow.com/search?q=How+to+make+php+https+request+and+read+response%3F+php – Gordon Jan 21 '11 at 11:33
  • possible duplicate of [Make a HTTPS request through PHP and get response](http://stackoverflow.com/questions/3873187/make-a-https-request-through-php-and-get-response) – Gordon Jan 21 '11 at 11:33

3 Answers3

5

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);
Anon434
  • 51
  • 1
  • 2
3

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://.

Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
2

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

symcbean
  • 47,736
  • 6
  • 59
  • 94