PHP Curl Multi post json data


PHP Curl Multi post json data

我在这个php curl多请求上被困了2个小时

我想让post-json数据从1111(这是一个起点,也是一个验证代码)开始到1121(终点1111+$process_count)

看看这个,伙计们:

<?php
$url = "https://api.mywebsite.com/myapp/customer/verification";
$mh = curl_multi_init();
$handles = array();
$process_count = 10;
for($c=1111;$c <= 1121;$c++){
  $data_verification = array(
      "phone" => "+6285643103039", // +6285643103039 9025
      "verificationCode" => $c
  );
  $str_verification = json_encode($data_verification);
}
while ($process_count--)
{
    $ch = curl_init($url);
    $headers= array('Accept: application/json','Content-Type: application/json');
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_TIMEOUT, 4000);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$str_verification);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    ob_start();
    curl_multi_add_handle($mh, $ch);
    $handles[] = $ch;
}
$running=null;
do
{
    curl_multi_exec($mh, $running);
}
while ($running > 0);
for($i = 0; $i < count($handles); $i++)
{
    $out = curl_multi_getcontent($handles[$i]);
    echo "$i. ";
    print $out . "'r'n";
    echo "<br>";
    curl_multi_remove_handle($mh, $handles[$i]);
}
curl_multi_close($mh);
?>

但是curl_setopt($ch,CURLOPT_POSTFIELDS,$str_verification);总是给定终点值1121。

并且不会循环

从1111到1121。

有人能搞清楚吗?我很乐意得到任何帮助

您在第一个循环中犯了一个错误,每次都将数据擦除到一个变量中,而不是数组中

  $str_verification = json_encode($data_verification);

以下是我建议你做的:

$str_verification = array();
for($c=1111;$c <= 1121;$c++){
  $data_verification = array(
      "phone" => "+6285643103039", // +6285643103039 9025
      "verificationCode" => $c
  );
  $str_verification[] = json_encode($data_verification);
}
for ($i = 0; $i != 10; $i++)
{
    $ch = curl_init($url);
    $headers= array('Accept: application/json','Content-Type: application/json');
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_TIMEOUT, 4000);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$str_verification[$i]);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    ob_start();
    curl_multi_add_handle($mh, $ch);
    $handles[] = $ch;
}