dongyi7041 2012-06-26 19:29
浏览 388
已采纳

Curl + proxy - 如果代理地址不可用,则重新加载脚本

I have a question - I'm using CURL and PROXY. I'm getting list with proxy addresses from paid web service. The list looks like this (example):

123.456.789.012:1234
123.456.789.012:1234
123.456.789.012:1234
123.456.789.012:1234
123.456.789.012:1234
123.456.789.012:1234
123.456.789.012:1234

Let's say, that the list mentioned above is available here: http://example.com/list_with_the_proxy

My PHP code looks like this:

<?php
$url = 'http://connect.to.another.example.net/'; //I want to open this url using one of the proxy address from the list

$proxy_url = 'http://address.of.proxy.example.org';

function RandomLine($filename) { 
    $lines = file($filename) ; 
    return $lines[array_rand($lines)] ; 
} 

$random_proxy = RandomLine($proxy_url); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1); 
curl_setopt($ch, CURLOPT_PROXY, $random_proxy); 
$data = curl_exec($ch); 
curl_close($ch); 
echo $data;

?>

I have noticed some problem - sometimes some proxy is unavailable, and the site is loading & loading & loading ... Is there any possibility, to reload the script and get new random proxy, if the site is loading too long ? For example if proxy is not responding in 5 seconds, script will load again ?

  • 写回答

2条回答 默认 最新

  • duanbi8089 2012-06-26 20:02
    关注

    Look into CURLOPT_TIMEOUT, reload in every case when you don't get any data in a reasonable time. Also are you sure that the data from the proxy list site is coming through correctly and its not hanging because of that?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器