我會說使用pfsockopen()
來創建一個持久連接將使伎倆。您可以打開HTTP API服務器的套接字,然後進行多個請求,並在頁面加載結束時關閉套接字。
<?php
$host = "protocol://your-api-hostname.com/";
$uri = "/your/api/location";
$port = 80;
// For an HTTP API, for example:
$socket = pfsockopen ($host, $port, $errno, $errstr, 0);
if (!$socket) {
echo $errno. " - " . $errstr;
} else {
// You can do here as many requests as you want.
fputs ($socket, $request1);
fputs ($socket, $request2);
// And then keep on reading until the end of the responses
$i = 0;
$a = array();
while (!feof($socket)) {
$a[$i] = fgets($socket);
$i++;
}
fclose($socket);
我不知道這是否正是您所需要的,但它提供了比cURL更多的選項。
要做到這一點,你需要有自己的中間人,可以保持與API提供者的聯繫。查看本地緩存API結果可能更容易一些嗎? –
它們很醜,但是框架可以成爲解決方案。 – MrYellow