当前位置: 动力学知识库 > 问答 > 编程问答 >

http - Regular curl get request php , why it's very slow?

问题描述:

I'm using cURL to make HTTP requests:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "some-site-without-many-photos");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$output = curl_exec ($ch);

I need to do this request in loop up to 200 times and get data. I looped for 10 times and it takes more than 25 seconds ... is this normal ? The program is very slow. A few years ago, I did this in C and I remember it's was a lot faster. Any idea make my request faster? Maybe way to get only text from the page?

网友答案:

Start measuring something. You can profile the total request time...

$start = gettimeofday(true);
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "some-site-without-many-photos"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$output = curl_exec ($ch); 
$end = gettimeofday(true);

$request_time = $end - $start;

Alternatively, you can just measure the DNS lookup time to see if this is the issue:

$start = gettimeofday(true);
gethostbyname("some-site");
$end = gettimeofday(true);

$request_time = $end - $start;

Loop this through your 200 or so requests and see the times individually (detects problems with one request), or in aggregate.

If you're requesting pages across different hosts, then you're setting up and tearing down a TCP connection for each request which can take upwards of 500ms depending on your RTT.

Measure and you'll find the issue, although you may or may not be able to do anything about it.

分享给朋友:
您可能感兴趣的文章:
随机阅读: