简体   繁体   English

在 PHP 中,如何向使用与我的网站 (https) 不同协议 (http) 的另一个网站发出 curl 请求?

[英]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.我想从我的 https 网站向另一个仅使用 http 的网站发出 curl 请求。 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'] .但是如果你想使用相同的协议,你可以检查$_SERVER['HTTPS']

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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