简体   繁体   中英

PHP transfer data between 2 remote servers, what is fastest way?

I have Server A and Server B which exchanges some data. Server A on user request pull data from Server B using simple file_get_content with some params, so server B can do all task(database etc) and return results to A which formats and show to user. Everything is in PHP.

Now I am interested what is fastest way to to this? I made some test and average transfer time for average response from server B at (~0.2 sec). In that 0.2 sec, 0.1 sec. aprox. is Server B operational time (pulling data calling few databases etc) what mean that average transfer time for 50kb with is 0.1 sec. (servers are NOT in same network)

Should I try with:

  1. cURL insted of file_get_content ?
  2. Or to try to make whole thing with sockets( I never work work with sockets in PHP but I supose that easily can be done, on that way to skip web server )
  3. or something third?

I think that time can be 'found' on shortening connection establishing, since now, every request is new connection initiating (I mean on separate file_get_content calls, or I am wrong?)

Please give me your advices in which directions to try, or if you have some better solution I am listening.

Curl :

function curl($url)
{
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL,$url);
    curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
    curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec ($ch);
    curl_close($ch);
    return $result;
}

Sockets:

function sockets($host) {
$fp = fsockopen("www.".$host, 80, $errno, $errstr, 30);
  $out = "GET / HTTP/1.1\r\n";
  $out .= "Host: www.".$host."\r\n";
  $out .= "Connection: Close\r\n\r\n";
  fwrite($fp, $out);
  $f='';
  while (!feof($fp)) {
    $f .= fgets($fp, 1024);
  }
return $f;
}

file_get_contents

 function fgc($url){
       return file_get_contents($url);
    }

Multicurl

function multiRequest($data,$nobody=false,$options = array(), $oneoptions = array())
{
    $curls = array();
    $result = array();
    $mh = curl_multi_init();
    foreach ($data as $id => $d)
    {
        $curls[$id] = curl_init();
        $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
        curl_setopt($curls[$id], CURLOPT_URL,            $url);
        curl_setopt($curls[$id], CURLOPT_HEADER,         0);
        curl_setopt($curls[$id], CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curls[$id], CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($curls[$id], CURLOPT_USERAGENT,"Mozilla/5.0(Windows;U;WindowsNT5.1;ru;rv:1.9.0.4)Gecko/2008102920AdCentriaIM/1.7Firefox/3.0.4");
        //curl_setopt($curls[$id], CURLOPT_COOKIEJAR,'cookies.txt');
        //curl_setopt($curls[$id], CURLOPT_COOKIEFILE,'cookies.txt');
        //curl_setopt($curls[$id], CURLOPT_NOBODY, $nobody);

        if (!empty($options))
        {
            curl_setopt_array($curls[$id], $options);
        }
        if (!empty($oneoptions[$id]))
        {
            curl_setopt_array($curls[$id], $oneoptions[$id]);
        }
        if (is_array($d))
        {
            if (!empty($d['post']))
            {

                curl_setopt($curls[$id], CURLOPT_POST,       1);
                curl_setopt($curls[$id], CURLOPT_POSTFIELDS, $d['post']);
            }
        }
        curl_multi_add_handle($mh, $curls[$id]);
    }
    $running = null;
    do
    {
        curl_multi_exec($mh, $running);
    }
    while($running > 0);
    foreach($curls as $id => $content)
    {
        $result[$id] = curl_multi_getcontent($content);
        //echo curl_multi_getcontent($content);
        curl_multi_remove_handle($mh, $content);
    }
    curl_multi_close($mh);
    return $result;
}

Tests:

$url = 'example.com';
$start = microtime(1);
for($i=0;$i<100;$i++)
curl($url);
$end = microtime(1);
echo "Curl:".($end-$start)."\n";

$start = microtime(1);
for($i=0;$i<100;$i++)
fgc("http://$url/");
$end = microtime(1);
echo "file_get_contents:".($end-$start)."\n";

$start = microtime(1);
for($i=0;$i<100;$i++)
sockets($url);
$end = microtime(1);
echo "Sockets:".($end-$start)."\n";

$start = microtime(1);
for($i=0;$i<100;$i++)
$arr[]=$url;
multiRequest($arr);
$end = microtime(1);
echo "MultiCurl:".($end-$start)."\n";
?>

Results :

  • Curl: 5.39667105675
    file_get_contents: 7.99799394608
    Sockets: 2.99629592896
    MultiCurl: 0.736907958984

what is fastest way?

get your data on a flash drive.

Now seriously.
Come on, it's network that's slow . You cannot make it faster.
To make server A response faster, DO NOT request data from the server B. That's the only way.
You can replicate your data or cache it, or just quit such a clumsy setup at all.
But as long as you have to make a network lookup on each user' request, it WILL be slow. Despite of the method you are using. It is not hte method, it is media. Isn't it obvious?

You can try another different approach: Mount the remote filesystem in the local machine. You can do that with sshfs , so you will get the additional security of an encripted connection.

It may be even more efficient since php will not have to deal with connection negotiation and establishment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM