Home >Backend Development >PHP Tutorial >php curl 用代理访问,服务器可以,本地不行

php curl 用代理访问,服务器可以,本地不行

WBOY
WBOYOriginal
2016-06-23 13:44:572154browse

测试用 curl代理访问,在服务器测试可行,本地却显示空白的,请教下是什么情况,代理ip是网上找的,可用

function curl_string ($url,$user_agent,$proxy){       $ch = curl_init();       curl_setopt ($ch, CURLOPT_PROXY, $proxy);       curl_setopt ($ch, CURLOPT_URL, $url);       curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);       curl_setopt ($ch, CURLOPT_COOKIEJAR, "c:\cookie.txt");       curl_setopt ($ch, CURLOPT_HEADER, 1);       curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);       curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);       curl_setopt ($ch, CURLOPT_TIMEOUT, 120);       $result = curl_exec ($ch);       curl_close($ch);       return $result;}$url = "http://www.baidu.com";$user_agent = "Mozilla/4.0";$proxy = "http://111.13.109.53:80";$string = curl_string($url,$user_agent,$proxy);echo $string; 


回复讨论(解决方案)

//数据发送function curl_post($api_server, $request, $proxy = 0,$header=array()) {	$ch = curl_init ( $api_server );	curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'POST' );	if(is_array($header) && !empty($header)){		curl_setopt ( $ch, CURLOPT_HEADER, 1 ); //设置header		curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );	}	curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );	curl_setopt ( $ch, CURLOPT_POSTFIELDS, $request ); // $request=json	if ($proxy) {		//通过代理请求		$API_CURLOPT_PROXY = "代理IP";		curl_setopt ( $ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP );		curl_setopt ( $ch, CURLOPT_PROXY, $API_CURLOPT_PROXY );	}	$result = curl_exec ( $ch );	$curl_errno = curl_errno ( $ch );	$curl_code = curl_getinfo ( $ch, CURLINFO_HTTP_CODE ); // 获取http返回值	curl_close ( $ch );	if ($curl_code == 200) {		return $result;	} else {		return FALSE;	}}

本地又没代理

错误时超时么?我也发生过这类问题,是超时,如果你也是这错误,我倒是可以提点建议

//数据发送function curl_post($api_server, $request, $proxy = 0,$header=array()) {	$ch = curl_init ( $api_server );	curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'POST' );	if(is_array($header) && !empty($header)){		curl_setopt ( $ch, CURLOPT_HEADER, 1 ); //设置header		curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );	}	curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );	curl_setopt ( $ch, CURLOPT_POSTFIELDS, $request ); // $request=json	if ($proxy) {		//通过代理请求		$API_CURLOPT_PROXY = "代理IP";		curl_setopt ( $ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP );		curl_setopt ( $ch, CURLOPT_PROXY, $API_CURLOPT_PROXY );	}	$result = curl_exec ( $ch );	$curl_errno = curl_errno ( $ch );	$curl_code = curl_getinfo ( $ch, CURLINFO_HTTP_CODE ); // 获取http返回值	curl_close ( $ch );	if ($curl_code == 200) {		return $result;	} else {		return FALSE;	}}

本地又没代理



这个方法测试了,还是显示空白

错误时超时么?我也发生过这类问题,是超时,如果你也是这错误,我倒是可以提点建议



不是超时,是执行结果为空白,啥都没

111.13.109.53 北京市 移动
ping 不通
访问 http://111.13.109.53:80 报不存在

显然是一个受保护的 ip

访问受保护的ip肯定不行的,这个ip确实ping不通

localhost:devbox lijunpeng$ ping 111.13.109.53 PING 111.13.109.53 (111.13.109.53): 56 data bytesRequest timeout for icmp_seq 0Request timeout for icmp_seq 1Request timeout for icmp_seq 2Request timeout for icmp_seq 3

111.13.109.53 北京市 移动
ping 不通
访问 http://111.13.109.53:80 报不存在

显然是一个受保护的 ip



这个是代理用的ip,在服务器上使用是有效的,可以获得目标地址的内容,在本机使用显示空白页,获取不到

访问受保护的ip肯定不行的,这个ip确实ping不通



访问的是百度,那个ip是代理用的ip.

代理ip都ping不通你怎么可能根据代理来访问百度呢?

这个ip要不就是你服务器的一个内网ip

本地curl开启了吗?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn