ForLoop 用于多个 API 数据推送 - 使用代码 PHP


ForLoop for Multiple API data Pushes - With Code PHP

我正在尝试多次将数据发送到服务器(对于几个不同的事务),但 for 循环不起作用。一个请求通过正常,没有问题,我得到了响应,但它不会遍历整个数组。我错过了一些简单的东西吗?

谢谢。.PHP:

$array 2 = 数组();

for ($i = 0; $i<=$numt; $i+=1) {
$arr = array('size' => $btcper, 'price' => $array[$i], 'side' => 'sell',
 'product_id' => 'BTC-USD');
array_push($array2, $arr);
};
for ($z = 0; $z<=$numt; $z+=1){
$output = json_encode($array2[$z]);

$key = $tkey;
$secret = $skey;
$passphrase = $passphrase1;
$time = time();
$url = "https://api.exchange.coinbase.com/orders";
$data = $time."POST"."/orders";
echo $data . "<br/>";
$hashinput = "$data"."$output";
$sign = base64_encode(hash_hmac("sha256", $hashinput, base64_decode($secret), true));                
echo $sign . "<br/>";
$headers = array(                
    'CB-ACCESS-KEY: '.$key,
    'CB-ACCESS-SIGN: '.$sign,
    'CB-ACCESS-TIMESTAMP: '.$time,
    'CB-ACCESS-PASSPHRASE: '.$passphrase,
    'Content-Type: application/json'
);

echo $url;
static $ch = null;
if (is_null($ch)) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'local server');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $output);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    $res = curl_exec($ch)."'n"."'n";
    echo $res;
$myFile = "idfilesell".date('Y-m-d').".txt";
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, $res);
fclose($fh);

}exit;
};
?>

接口响应:

https://api.exchange.coinbase.com/orders{"id":"41c4414c-8ec0-48f6-ad36-    25f0689c71a8","price":"300.00000000","size":"0.01000000","product_id":"BTC-USD","side":"sell","stp":"dc"}

我认为coinbase具有相当复杂的DOS保护。也许你因为太快提出请求而击中了它。尝试在循环中放置一些等待(例如一秒钟),看看会发生什么。