我有 PHP 代碼可以使用不同的資料對同一個 URL 執行 2 個 Curl 呼叫。它有效,但第一次和第二次之間有一些滯后(幾秒鐘)。我看過 curl multi 但無法弄清楚如何將它用于 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);
uj5u.com熱心網友回復:
您打開 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);
uj5u.com熱心網友回復:
您可以使用同時啟動它們stream_socket_client()
您也可以使用 stream_socket_client() 來呼叫您自己的兩個 php 腳本,其中每個 curl 請求都在每個腳本中。然后,您使用您自己的腳本使用的 url
呼叫您的兩個腳本。stream_socket_client()
使用套接字并不簡單,所以我附上了我 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=(detect automatically)&doctype=Inline&group=0&output=json");
$urls[] = array('host' => "validator.w3.org",'path' => "/check?uri=$url&charset=(detect automatically)&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;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/505196.html
