简体   繁体   中英

PHP Curl Multi post json data

I got stuck for 2 hours on this php curl multiple request

i wantta make post json data start from 1111(This is a start point and as a verificationCode) to 1121(end point 1111 + $process_count)

check this out guys :

<?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);
?>

But curl_setopt($ch, CURLOPT_POSTFIELDS,$str_verification); always given end point value 1121.

And doesn't looping

from 1111 to 1121.

Anyone can figure it out ? i'll glad for any help

You are doing a mistake in your first loop, you are erasing data every time into only one variable and not array

  $str_verification = json_encode($data_verification);

Here is what I suggest you to do :

$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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM