Make a HTTPS request through PHP and get response

后端 未结 4 832
青春惊慌失措
青春惊慌失措 2020-12-01 16:50

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.c         


        
相关标签:
4条回答
  • 2020-12-01 17:20

    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>";
    ?>
    
    0 讨论(0)
  • 2020-12-01 17:21

    There are 2 example GET Method And POST Method

    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;
    }
    ?>
    
    0 讨论(0)
  • 2020-12-01 17:23

    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/');
    
    0 讨论(0)
  • 2020-12-01 17:40

    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/

    0 讨论(0)
提交回复
热议问题