How to get the value of a curl and play for another curl

0

Colleagues.

I have a curl which when running brings this value:

  

login: Fernando Pessoa, password: x2cz

But I need to get that value and play into another curl so that it is logged in automatically. I'm trying this way:

$output = curl_exec($curl); // resultado da curl anterior a essa

    $obj = json_decode($output,true);

    curk_init();
    curl_setopt($curl, CURLOPT_URL, "https://site.com.br/");
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($obj))
    );
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  
    curl_setopt($curl, CURLOPT_POSTFIELDS, $obj); 
    $resultado = curl_exec($curl);
    echo $resultado;
    
asked by anonymous 10.05.2016 / 21:35

1 answer

1

Extract the data with regex and then mount the second curl with the POST parameters based on these data:

<?php

// Primeiro CURL, obtém dados de login
$output = curl_exec($curl);
$obj = json_decode($output,true);

// extrai a senha
$msg = $obj->responsavel[0]->mensagem;
preg_match("/senha: (.+)/", $msg, $matches);
$senha = $matches[1];


// Faz segundo CURL

$data_string = json_encode(array(
    // Monte o POST conforme for necessário
    // Esse é um exemplo, já que vc não especificou
    'nome' => $obj->nome,
    'senha' => $senha
));

curl_init();
curl_setopt($curl, CURLOPT_URL, "https://site.com.br/");
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string); 
$resultado = curl_exec($curl);
echo $resultado;

?>
    
10.05.2016 / 21:48