2016-03-27 70 views
1

我被困關於這個PHP2小時捲曲多個請求PHP捲曲多交的JSON數據

我wantta使後JSON數據從1111開始(這是一個起點和一個verificationCode)到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

任何人都可以計算出來?我會很高興的任何幫助

回答

0

你正在做你的第一個循環中的錯誤,你每次擦除數據到只有一個變量,而不是數組

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