开发者

Why does my socks5 speed measure with usage of PHP and cURL not work?

I want to measure the speed of a socks5 server via PHP so I wrote the following code:

$url = 'http://cachefly.cachefly.net/1mb.test';
$proxy = '126.XXX.XXX.XXX:1080’;
//$auth  = $username.':'.$password;

$fp = fopen('1mb.test', 'w');

$start = microtime(true);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, $auth);

$result = curl_exec($ch);

curl_close($ch);

$end = microtime(true);

fclose($fp);

$diff = $end - $start;
echo "elapsed time: ".$diff." seconds";

$a = 1024 / $diff;
echo '<br>'.floor($a).' kb/s';

Unfortunately the code is not working. Without the usage of a socks5 server the file gets downloaded completely - otherwise not. Do开发者_如何学Goes anybody of you know why the code isn't working? Are there any alternatives or suggestions on how I can accomplish my goal?

Sincerely, Said.


"without the socks5 server the file gets downloaded". In other words, the proxy isn't working?

You're not checking for curl errors. Add the following to your code to see why the download isn't succeeding:

$result = curl_exec($ch);
if ($result === FALSE) {
   die(curl_error($ch));
}

Never assume that some code dealing with an external resource will suceed. Your code might be perfect, but there's far too many reasons for the external resource to fail to NOT check for success/failure each time.


It is impossible to tell without lots of more details. Exactly how does it fail? Does it always fail? Also, you should make sure that you use a reasonably recent libcurl version so that you're not experiencing old bugs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜