简体   繁体   中英

In PHP how do I do a curl request to another website that uses a different protocol (http) than my website (https)?

I want to do a curl request from my https website to another website that only uses http. How do I do that? The code below only works if the protocols are the same. How do I modify these two examples to make it work across the different protocols, if possible? Thank you for your help.

<?php
    // create curl resource 
    $ch = curl_init(); 

    // set url 
    curl_setopt($ch, CURLOPT_URL, 'http://www.anotherwebsite.com'); 

    //return the transfer as a string 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

    // $output contains the output string 
    $output = curl_exec($ch); 

    // close curl resource to free up system resources 
    curl_close($ch);

    // OR I WANT TO USE
    $result = file_get_contents('http://www.anotherwebsite.com');
?>

I can't think of any reason why it should be necessary. But if you want to use the same protocol, you can check $_SERVER['HTTPS'] .

$protocol = empty($_SERVER['HTTPS']) ? 'http' : 'https';
$url = "$protocol://www.anotherwebsite.com";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM