[英]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.