通过代理使用https请求的file_get_contents [英] file_get_contents with https requests via proxy
问题描述
如何通过代理服务器进行HTTPS请求
How to do HTTPS requests via proxy server
代理服务器在debian上 tinyproxy
The proxy server is tinyproxy
on debian
$context = stream_context_create([
'http' => [
'proxy' => 'tcp://xx.xx.xx.xx:8888',
'request_fulluri' => true
]
]);
echo file_get_contents('https://www.google.com', false, $context);
错误
error
Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol in C:\wamp\www\google\test.php on line 10
Warning: file_get_contents(https://www.google.com): failed to open stream: Cannot connect to HTTPS server through proxy in C:\wamp\www\google\test.php on line 10
推荐答案
我推荐cURL去做这个。在stackoverflow上的某个地方,用户说这个并且我完全同意。
I would recommend cURL to do this. Somewhere on stackoverflow a user said this and I totally agree.
file_get_contents()是一个简单的螺丝刀。非常适合简单的GET
请求,其中标头,HTTP请求方法,超时,cookiejar,
重定向和其他重要事项无关紧要。带有setopt
的cURL是一个几乎所有你能想到的选项的powerdrills。
file_get_contents() is a simple screwdriver. Great for simple GET requests where the header, HTTP request method, timeout, cookiejar, redirects, and other important things do not matter. cURL with setopt is a powerdrills with almost every option you can think of.
<?php
$url = 'https://www.google.com';
// to check your proxy
// $url = 'http://whatismyipaddress.com/';
$proxy = '50.115.194.97:8080';
// create curl resource
$ch = curl_init();
// set options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // read more about HTTPS http://stackoverflow.com/questions/31162706/how-to-scrape-a-ssl-or-https-url/31164409#31164409
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
echo $output;
?>
这篇关于通过代理使用https请求的file_get_contents的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!