PHP's cURL: How to connect over HTTPS?

前端 未结 5 1208
余生分开走
余生分开走 2021-02-15 12:23

I need to do a simple GET request to EC2 Query API with regular URL encoded query string. The protocol is HTTPS. How would I send the request with the help of PHP\'s cURL.

相关标签:
5条回答
  • 2021-02-15 12:49

    This code can use to retrieve https requests

    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL,'https://serverurl.com/');
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $data1 = curl_exec($ch);
    curl_close($ch);
    
    0 讨论(0)
  • 2021-02-15 12:51

    Example:

    $url = "https://example.com";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
    
    $result = curl_exec($ch);
    curl_close($ch);
    
    print_r($result);
    

    CURLOPT_SSL_VERIFYPEER

    Check if peer certificate is valid or invalid/expired.

    CURLOPT_SSL_VERIFYHOST quoting from php manual:

    1 to check the existence of a common name in the SSL peer certificate. 2 to check the existence of a common name and also verify that it matches the hostname provided.

    0 讨论(0)
  • 2021-02-15 13:06

    You could also use Zend Framework and the cURL adapter to help with this task. Details here

    0 讨论(0)
  • 2021-02-15 13:12

    Sending a request via curl, to an HTTPS URL, is not that hard by itself, in terms of PHP code.

    Something like this should do perfectly fine (I just tried this portion of code on my machine, Windows, PHP 5.3) :

    $url = 'https://.../...';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($ch);
    curl_close($ch);
    
    echo $data;
    

    And it outputs the result fine : the same thing I get in my browser when trying to access the https:// URL ; except for the CSS, of course.


    You might want to take a look at the manual page of the curl_setopt function : there are a lot of options, and some of those might be useful, in your specific case :-)

    Here, I used CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST ; not sure you'll need those with Amazon, but I had to use them, else this portion of code didn't work -- but that might be related to the fact that the certificate I'm using is self-signed... Try with and without those, and you'll quickly find out if you need them.

    0 讨论(0)
  • 2021-02-15 13:12

    If you want to configure CURL to blindly accept the certificate you can set the CURLOPT_SSL_VERIFYPEER option to false.

    $url = 'https://www.example.com/abc';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    // Blindly accept the certificate
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    
    $response = curl_exec($ch);
    curl_close($ch);
    
    var_dump($response);
    
    0 讨论(0)
提交回复
热议问题