Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a HTTPS request through PHP and get response

Tags:

php

https

request

I want to make HTTPS request through PHP to a server and get the response.

something similar to this ruby code

  http = Net::HTTP.new("www.example.com", 443)

  http.use_ssl = true

  path = "uri"

  resp, data = http.get(path, nil)

Thanks

like image 713
wael34218 Avatar asked Oct 06 '10 13:10

wael34218


2 Answers

this might work, give it a shot.

 $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);

for more info, check http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/

like image 145
saadlulu Avatar answered Nov 04 '22 07:11

saadlulu


The Zend Framework has a nice component called Zend_Http_Client which is perfect for this kind of transaction.

Under the hood it uses curl to make requests, but you'll find Zend_Http_Client has a much nicer interface to work with and is easier to configure when you want to add custom headers or work with responses.

If all you want to do is retrieve the page contents with minimal work, you may be able to do the following, depending on your server's configuration:

$data = file_get_contents('https://www.example.com/');
like image 38
David Snabel-Caunt Avatar answered Nov 04 '22 06:11

David Snabel-Caunt