簡體   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)?

我想從我的 https 網站向另一個僅使用 http 的網站發出 curl 請求。 我怎么做? 下面的代碼僅在協議相同時才有效。 如果可能,如何修改這兩個示例以使其適用於不同的協議? 感謝您的幫助。

<?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');
?>

我想不出任何必要的理由。 但是如果你想使用相同的協議,你可以檢查$_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