我试图在php中使用这两种方法使用代理。我有socks5代理。使用cURL
方法时效果很好。但是,当我尝试使用stream_context_set_default
时,它根本不起作用。有没有办法使用代理?例如:此代理207.174.151.226:45021
不能与stream_context_set_default
一起使用,但可以使用cURL。我不想使用cURL使用代理,因为我的php文件中到处都需要代理。对此有何帮助?代码:
<?php
$default_opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar",
'proxy'=>"186.34.59.99:10000"
)
);
$default = stream_context_set_default($default_opts);
$homepage = file_get_contents('http://www.google.com');
echo $homepage;
?>
这有效:
<?php
$url = "http://www.google.co.uk";
$ch = curl_init();
if($ch === false)
{
die('Failed to create curl object');
}
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
//proxy details
curl_setopt($ch, CURLOPT_PROXY, '186.34.59.99:10000');
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
?>
谢谢!
答案 0 :(得分:-1)
确保发送包含原始主机名的URI。您可以通过request_fulluri = true
$default_opts['http']
中明确传递public static void main (String[] args)
{
String str = "hello I am a example example";
String[] tokens = str.split(" ");
Set<String> set = new HashSet<>();
List<String> list = new ArrayList<>();
for(String token : tokens) {
if(!set.contains(token)) {
set.add(token);
list.add(token);
}
}
/* Print String */
for(int i = 0; i < list.size(); i++)
System.out.print(list.get(i) + " ");
}
来实现此目标,如http://php.net/manual/en/context.http.php