Make a HTTPS request through PHP and get response

By | February 23, 2018

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

something similar to this ruby code

  http ="", 443)

  http.use_ssl = true

  path = "uri"

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



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);

for more info, check


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('');


Example how to use HttpRequest to post data and receive the response:

//set up variables
$theData = '<?xml version="1.0"?>
    <to>my brother</to>
    <body>this is my body</body>
$url = '';
$credentials = '';
$header_array = array('Expect' => '',
                'From' => 'User A');
$ssl_array = array('version' => SSL_VERSION_SSLv3);
$options = array(headers => $header_array,
                httpauth => $credentials,
                httpauthtype => HTTP_AUTH_BASIC,
            protocol => HTTP_VERSION_1_1,
            ssl => $ssl_array);

//create the httprequest object               
$httpRequest_OBJ = new httpRequest($url, HTTP_METH_POST, $options);
//add the content type
$httpRequest_OBJ->setContentType = 'Content-Type: text/xml';
//add the raw post data
$httpRequest_OBJ->setRawPostData ($theData);
//send the http request
$result = $httpRequest_OBJ->send();
//print out the result
echo "<pre>"; print_r($result); echo "</pre>";


There are 2 example GET Method And POST Method

GET example:

$r = new HttpRequest('', HttpRequest::METH_GET);
$r->setOptions(array('lastmodified' => filemtime('local.rss')));
$r->addQueryData(array('category' => 3));
try {
    if ($r->getResponseCode() == 200) {
        file_put_contents('local.rss', $r->getResponseBody());
} catch (HttpException $ex) {
    echo $ex;

Post Example

$r = new HttpRequest('', HttpRequest::METH_POST);
$r->setOptions(array('cookies' => array('lang' => 'de')));
$r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
$r->addPostFile('image', 'profile.jpg', 'image/jpeg');
try {
    echo $r->send()->getBody();
} catch (HttpException $ex) {
    echo $ex;

Leave a Reply

Your email address will not be published. Required fields are marked *