2
I have this code for an API test and I wanted each end of the loop to write on the screen the progress of the script in %, but it only shows when it finishes doing everything, there is some way to make it work ? .
<?php
require __DIR__ . '/vendor/autoload.php';
//url do destino da requsiçao, equivalente ao "action" de um formulário
$url = 'localhost:8000/api/aluno/register';
// Faker
$faker = Faker\Factory::create();
$resultados = array();
for ($i=1; $i <= 200; $i++) {
//estes seriam os "inputs" do formulário
$campos = array(
'nome'=>urlencode($faker->name),
'telefone'=>urlencode("4321"),
'objetivo'=>urlencode("Ficar Monstro"),
'email'=>urlencode($faker->email),
'endereco'=>urlencode($faker->address),
'numero'=>urlencode($faker->numberBetween(1,800)),
'bairro'=>urlencode($faker->city),
'nascimento'=>urlencode($faker->dateTimeThisCentury->format('Y-m-d')),
'vencimento'=>urlencode($faker->numberBetween(1,28)),
'imgName'=>urlencode("default.png"),
'status'=>urlencode("ativo"),
'password'=>urlencode($faker->password),
'status_mensalidade'=>urlencode("Pago"),
'atividade_id'=>urlencode($faker->numberBetween(1,8)),
'tipo_atividade'=>urlencode("Individual"),
'valorPago'=>$faker->numberBetween(1,40),
);
// Header
$header = array();
$header[] = 'Content-type: application/x-www-form-urlencoded';
$header[] = 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOi8vbG9jYWxob3N0OjgwMDAvYXBpL2F1dGgvbG9naW4iLCJpYXQiOjE1MTY0MTA0NDgsImV4cCI6MTUxNjQ0MDQ0OCwibmJmIjoxNTE2NDEwNDQ4LCJqdGkiOiIySHNzS3BDb1Z5cnRLcDlGIiwic3ViIjo0MCwicHJ2IjoiODdlMGFmMWVmOWZkMTU4MTJmZGVjOTcxNTNhMTRlMGIwNDc1NDZhYSJ9.AWaghE64hQv-iix6Lmd2id7d669MfKeA1tot_GN2kUU';
//temos que colocar os parâmetros do post no estilo de uma query string
$string_campos = null;
foreach($campos as $name => $valor) {
$string_campos .= $name . '=' . $valor . '&';
}
$string_campos = rtrim($string_campos,'&');
$ch = curl_init();
//configurando as opções da conexão curl
curl_setopt($ch,CURLOPT_URL,$url);
//este parâmetro diz que queremos resgatar o retorno da requisição
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch,CURLOPT_POST,count($campos));
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
curl_setopt($ch,CURLOPT_POSTFIELDS,$string_campos);
//manda a requisição post
$resultado = curl_exec($ch);
curl_close($ch);
echo $i/200 . '%';
array_push($resultados,$resultado);
}
var_dump($resultados);
?>
In classic Asp I used a flush, must have something similar to php
– Leandro Angelo