PHP代理访问网络资源

时间:2022-06-17 14:30:56

第一种:使用DOMDocument

 <? PHP
$doc = new \DOMDocument();
$opts = array(
    'http' => array(
        'user_agent' => 'PHP libxml agent',
        'timeout' => 5, 
'proxy' => 'tcp://10.41.70.16:80', 
'request_fulluri' => True
        )
        );
$context = stream_context_create($opts);
libxml_set_streams_context($context);
$result = $doc->load($url); 
?>
第二种:使用file_get_contents的代理方法
<?php 
$url = "http://www.jb51.net/"; 
$ctx = stream_context_create(array( 
'http' => array('timeout' => 5, 
'proxy' => 'tcp://60.175.203.243:8080', 
'request_fulluri' => True,) 
); 
$result = file_get_contents($url, False, $ctx); 
echo $result; 
?> 
第三种:curl 的方式使用代理的方法
function postPage($url) 
$response = ""; 
$rd=rand(1,4); 
$proxy='http://221.214.27.253:808'; 
if($rd==2) $proxy='http://222.77.14.56:8088'; 
if($rd==3) $proxy='http://202.98.123.126:8080'; 
if($rd==4) $proxy='http://60.14.97.38:8080'; 
if($url != "") { 
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_PROXY, $proxy); 
$response = curl_exec($ch); 
if(curl_errno($ch)) $response = ""; 
curl_close($ch); 
return $response;