How to take the value of a Curl and play to another Curl

Asked

Viewed 542 times

0

Colleagues.

I have a Curl which when running brings this value:

Login: Fernando Pessoa, password: x2cz

But I need to take this value and play inside another Url to be automatically logged in. 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;
  • The variable $output stores the value login: Fernando Pessoa, senha: x2cz? If yes, why are you using the function json_decode in a string that is not a JSON? The intention would be to generate an associative array? Describe how the POST of the second Curl should be.

  • Actually the login value: Fernando Pessoa, password: x2cz comes from another URL I’m using, which takes the values of a json. But I need to take this value and access the other site already logged in.

  • Would $output = curl_exec($Curl); from the previous Curl

  • You need to clarify: 1) what the content of the $obj variable (check with var_dump) and 2) is like the POST you want to mount for the second request (I suppose it’s something like login=Fernando Pessoa&senha=z2cz).

  • Actually I don’t even think you need json. I gave a print_r and returned it (abbreviated to take only the data I need). {"name":"Fernando Pessoa","responsavel":[{"email":"[email protected]","message":"Successfully registered, password: bns7"}],

1 answer

1


Extract the data with regex and then assemble the second URL 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;

?>
  • Forgive me whirling. I misexpressed the question. I changed to be clearer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.