PHP文件中的2个JSON Curl请求是否按顺序排列?

o75abkj4  于 2022-11-13  发布在  PHP
关注(0)|答案(2)|浏览(105)

我有PHP代码执行2个Curl调用相同的URL与不同的数据。它的工作,但有一些滞后(几秒钟)之间的第一和第二。我已经看了curl多,但不能弄清楚如何使用它的JSON数组。请帮助?

<?php
$data = array("epic" => $epic, "expiry" => "DFB");
$data_string = json_encode($data);

$headers = array(
'Content-Type: application/json; charset=UTF-8',
'Accept: application/json; charset=UTF-8',
'X-IG-API-KEY: '.$xapikey,
'Version: 1',
'X-SECURITY-TOKEN: '.$_SESSION['api_xtoken'],
'CST: ' .$_SESSION['api_cst'],
'_method: DELETE',

);
$ch = curl_init('' . $trading_url . '');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);

    
$data2 = array("currencyCode" => "GBP", "direction" => $tv_direction);
$data_string2 = json_encode($data2);

$ch2 = curl_init('' . $trading_url . '');
curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_VERBOSE, 1);
curl_setopt($ch2, CURLOPT_HEADER, 1);
curl_setopt($ch2, CURLOPT_POSTFIELDS,$data_string2);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_HTTPHEADER, $headers2);

$result2 = curl_exec($ch2);
dzhpxtsq

dzhpxtsq1#

您打开curl连接两次,而且您还没有关闭连接,一旦请求完成,您需要关闭连接
我可以试试这样的东西

$data_string = json_encode($data);

$headers = array(
'Content-Type: application/json; charset=UTF-8',
'Accept: application/json; charset=UTF-8',
'X-IG-API-KEY: '.$xapikey,
'Version: 1',
'X-SECURITY-TOKEN: '.$_SESSION['api_xtoken'],
'CST: ' .$_SESSION['api_cst'],
'_method: DELETE',

);
$ch = curl_init('' . $trading_url . '');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);

    
$data2 = array("currencyCode" => "GBP", "direction" => $tv_direction);
$data_string2 = json_encode($data2);

curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers2);

$result2 = curl_exec($ch); 
    curl_close($ch);
knpiaxh1

knpiaxh12#

您可以使用stream_socket_client()同时启动它们
您还可以使用stream_socket_client()调用您自己的两个php脚本,其中每个curl请求都在每个脚本中。
然后,使用stream_socket_client()调用两个脚本,其中使用的URL指向您自己的脚本。
使用套接字并不简单,所以我在其中加入了我10年前写的一些代码,据我所知,它花了一两天的时间才能运行
这个脚本使用W3C CSS和HTML验证器工具来验证网页,并通过网页速度测试来评估网页的设计质量。与串行请求相比,并行调用它们可以显著减少时间。
您可能需要使用较新的用户代理。

ini_set('user_agent', 'Mozilla/5.0 (Windows NT 5.1; rv:22.0) Gecko/20100101 Firefox/22.0');
  set_time_limit (60);
  $timeout = 120; 
  $result = array(); 
  $sockets = array(); 
  $buffer_size = 8192;
  $id = 0;
  $urls = array();
  $path = $url;
  $url = urlencode("$url");
  $urls[] = array('host' => "jigsaw.w3.org",'path' => "/css-validator/validator?uri=$url&profile=css3&usermedium=all&warning=no&lang=en&output=text");
  $urls[] = array('host' => "validator.w3.org",'path' => "/check?uri=$url&charset=%28detect+automatically%29&doctype=Inline&group=0&output=json");
  $urls[] = array('host' => "validator.w3.org",'path' => "/check?uri=$url&charset=%28detect+automatically%29&doctype=XHTML+Basic+1.1&group=0&output=json");
  $urls[] = array('host' => "www.webpagetest.org",'path' => "/runtest.php?f=xml&bwDown=10000&bwUp=1500&latency=40&fvonly=1&k=1a736137bdd4ea914a6fc7ca06f71a&url=$url");
  $err = '';
  foreach($urls as $path){
    $host = $path['host'];
    $path = $path['path'];
    $http = "GET $path HTTP/1.0\r\nHost: $host\r\n\r\n";
  //  echo "<br/>\n=>$host<br/>\n=>$path<br/>\n=>$http<br/>\n";
    $stream = stream_socket_client("$host:80", $errno,$errstr, 120,STREAM_CLIENT_ASYNC_CONNECT|STREAM_CLIENT_CONNECT); 
    if ($stream) {
      $sockets[] = $stream;  // supports multiple sockets
      $start[] = microtime(true);
      fwrite($stream, $http);
    }
    else { 
   $err .=  "$id Failed<br>\n";
    }
  }

while (count($sockets)) {
  $read = $sockets; 
  stream_select($read, $write = NULL, $except = NULL, $timeout);
  if (count($read)) {
    foreach ($read as $r) { 
      $id = array_search($r, $sockets); 
      $data = fread($r, $buffer_size); 
      if (strlen($data) == 0) { 
     //   echo "$id Closed: " . date('h:i:s') . "\n\n\n";
        $closed[$id] = microtime(true);
        fclose($r); 
        unset($sockets[$id]);
      } 
      else {
        $result[$id] .= $data; 
      }
    }
  }
  else { 
 //   echo 'Timeout: ' . date('h:i:s') . "\n\n\n";
    break;
  }
}

相关问题