Heim >Backend-Entwicklung >PHP-Tutorial >php curl 用代理访问,服务器可以,本地不行
测试用 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确实ping不通
代理ip都ping不通你怎么可能根据代理来访问百度呢?
这个ip要不就是你服务器的一个内网ip
本地curl开启了吗?