12

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

Ashesh Kumar
  • 223
  • 2
  • 12
wael34218
  • 4,860
  • 8
  • 44
  • 62

5 Answers5

19

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/

saadlulu
  • 1,375
  • 2
  • 26
  • 38
  • 1
    The link was perfect! You could update your answer to have at least the curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); line – rsc Oct 08 '14 at 07:35
3

To open HTTPS stream I could only work it out using both HTTP and SSL context options…

Thus something like:

<?php
$postdata = http_build_query(
    array(
        'var1' => 'some content',
        'var2' => 'doh'
    )
);

$opts = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peername' => false
        // Instead ideally use
        // 'cafile' => 'path Certificate Authority file on local filesystem'
    ),
    'http' => array(
        'method' => 'POST',
        'header' => 
            'Content-type: application/x-www-form-urlencoded'."\r\n".
            ''
        'content' => $postdata
    )
);

$context = stream_context_create($opts);

$result = file_get_contents('https://example.com/submit.php', false, $context);

// A Good friend
var_dump($http_response_header);
?>
llange
  • 757
  • 2
  • 10
  • 14
2

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/');
David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
-1

There are 2 examples, GET and POST methods, but they both require the HttpRequest library to be included.

If you don't have the HttpRequest library included, kindly check this answer on how to include it: HttpRequest not found in php

GET example:

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

Post Example

<?php
$r = new HttpRequest('http://example.com/form.php', 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;
}
?>
Salam El-Banna
  • 3,784
  • 1
  • 22
  • 34
-1

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

<?php
//set up variables
$theData = '<?xml version="1.0"?>
<note>
    <to>my brother</to>
    <from>me</from>
    <heading>hello</heading>
    <body>this is my body</body>
</note>';
$url = 'http://www.example.com';
$credentials = 'user@example.com:password';
$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>";
?>
  • I get `Uncaught Error: Class 'HttpRequest' not found`. If this answer requires a library you need to provide more details. This is not a working example as far as I can tell. – Paul Wheeler Dec 19 '22 at 06:45