我有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);
2条答案
按热度按时间dzhpxtsq1#
您打开curl连接两次,而且您还没有关闭连接,一旦请求完成,您需要关闭连接
我可以试试这样的东西
knpiaxh12#
您可以使用
stream_socket_client()
同时启动它们您还可以使用stream_socket_client()调用您自己的两个php脚本,其中每个curl请求都在每个脚本中。
然后,使用
stream_socket_client()
调用两个脚本,其中使用的URL指向您自己的脚本。使用套接字并不简单,所以我在其中加入了我10年前写的一些代码,据我所知,它花了一两天的时间才能运行
这个脚本使用W3C CSS和HTML验证器工具来验证网页,并通过网页速度测试来评估网页的设计质量。与串行请求相比,并行调用它们可以显著减少时间。
您可能需要使用较新的用户代理。