我做错了什么?
testform.php
<html>
<form action="consulta.php" method="POST">
<label>CNS</label>
<input type="text" name="cns">
<label>CPF</label>
<input type="text" name="cpf">
<input type="submit">
</form>
</html>
consulta.php
<?php
ini_set( 'display_errors', 1 );
error_reporting(E_ALL);
$cpf = $_POST["cpf"];
$cns = $_POST["cns"];
if ($cns != null){
$lol = 1;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://cadastro.saude.gov.br/cadsusweb/login/actionArmazenarXS.form');
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
// curl_setopt($ch, CURLOPT_CAINFO, 'local/crt.crt');
curl_setopt($ch, CURLOPT_POSTFIELDS, "cnes=NAOPOSSOMOSTRAR&usuario=NAOPOSSOMOSTRAR&senha=NAOPOSSOMOSTRAR");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie-name'); //could be empty, but cause problems on some hosts
curl_setopt($ch, CURLOPT_COOKIEFILE, '/var/www/ip4.x/file/tmp'); //could be empty, but cause problems on some hosts
$answer = curl_exec($ch);
if (curl_error($ch)) {
echo curl_error($ch);
}
//another request preserving the session
curl_setopt($ch, CURLOPT_URL, 'https://cadastro.saude.gov.br/cadsusweb/j_security_check');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
// curl_setopt($ch, CURLOPT_CAINFO, 'local/crt.crt');
curl_setopt($ch, CURLOPT_POSTFIELDS, "j_username=NAOPOSSOMOSTRAR&cnes=NAOPOSSOMOSTRAR&usuario=NAOPOSSOMOSTRAR&j_password=NAOPOSSOMOSTRAR");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie-name'); //could be empty, but cause problems on some hosts
curl_setopt($ch, CURLOPT_COOKIEFILE, '/var/www/ip4.x/file/tmp'); //could be empty, but cause problems on some hosts
$answer = curl_exec($ch);
if (curl_error($ch)) {
echo curl_error($ch);
}
curl_setopt($ch, CURLOPT_URL, 'https://cadastro.saude.gov.br/cadsusweb/restrito/consultar/pesquisar.form?callback=');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
// curl_setopt($ch, CURLOPT_CAINFO, 'local/crt.crt');
curl_setopt($ch, CURLOPT_POSTFIELDS, "usuario=%7B%22idCorporativo%22%3Anull%2C%22idLocal%22%3Anull%2C%22desabilitarDataQuality%22%3Anull%2C%22obsDesabilitarDataQuality%22%3Anull%2C%22numeroProtocoloPrecadastro%22%3Anull%2C%22protocoloPrimeiroAcesso%22%3Anull%2C%22protocolo%22%3Anull%2C%22solicitarAcessoPortal%22%3Afalse%2C%22encontradoReceita%22%3Anull%2C%22cpf%22%3Anull%2C%22numeroCns%22%3A%22%22%2C%22nome%22%3A%22%22%2C%22nomeSocial%22%3A%22%22%2C%22nomeMae%22%3A%22%22%2C%22nomePai%22%3A%22%22%2C%22sexo%22%3Anull%2C%22sexoDescricao%22%3Anull%2C%22racaCor%22%3Anull%2C%22racaCorDescricao%22%3Anull%2C%22dataObito%22%3Anull%2C%22dataOperacaoObito%22%3Anull%2C%22motivoDeclaracaoObito%22%3Anull%2C%22cnesOperador%22%3Anull%2C%22tipoSanguineo%22%3Anull%2C%22etniaIndigena%22%3Anull%2C%22etniaIndigenaDescricao%22%3Anull%2C%22dataNascimento%22%3A%22%22%2C%22nacionalidade%22%3Anull%2C%22paisNascimentoCodigo%22%3Anull%2C%22paisNascimento%22%3Anull%2C%22municipioNascimentoCodigo%22%3A%22%22%2C%22municipioNascimento%22%3A%22%22%2C%22dataNaturalizacao%22%3Anull%2C%22portariaNaturalizacao%22%3Anull%2C%22dataEntradaBrasil%22%3Anull%2C%22emailPrincipal%22%3Anull%2C%22emailAlternativo%22%3Anull%2C%22emailPrincipalValidado%22%3Anull%2C%22emailAlternativoValidado%22%3Anull%2C%22telefone%22%3A%5B%5D%2C%22nomade%22%3Afalse%2C%22enderecoCodigo%22%3Anull%2C%22paisResidenciaCodigo%22%3Anull%2C%22paisResidenciaDescricao%22%3Anull%2C%22enderecoMunicipio%22%3Anull%2C%22enderecoMunicipioCodigo%22%3Anull%2C%22enderecoTipoLogradouro%22%3Anull%2C%22enderecoTipoLogradouroCodigo%22%3Anull%2C%22enderecoLogradouro%22%3Anull%2C%22enderecoNumero%22%3Anull%2C%22enderecoComplemento%22%3Anull%2C%22enderecoBairroCodigo%22%3Anull%2C%22enderecoBairro%22%3Anull%2C%22enderecoCep%22%3Anull%2C%22emailPrincipalCodigo%22%3Anull%2C%22emailAlternativoCodigo%22%3Anull%2C%22dnv%22%3Anull%2C%22numeroInscricaoSocialCodigo%22%3Anull%2C%22numeroInscricaoSocial%22%3Anull%2C%22rgCodigo%22%3Anull%2C%22rgNumero%22%3Anull%2C%22rgOrgaoEmissor%22%3Anull%2C%22rgOrgaoEmissorDescricao%22%3Anull%2C%22rgUf%22%3Anull%2C%22rgDataEmissao%22%3Anull%2C%22tituloEleitorCodigo%22%3Anull%2C%22tituloEleitorNumero%22%3Anull%2C%22tituloEleitorZona%22%3Anull%2C%22tituloEleitorSecao%22%3Anull%2C%22certidao%22%3A%5B%5D%2C%22ctpsCodigo%22%3Anull%2C%22ctpsNumero%22%3Anull%2C%22ctpsSerie%22%3Anull%2C%22ctpsDataEmissao%22%3Anull%2C%22cnhNumero%22%3Anull%2C%22cnhDataEmissao%22%3Anull%2C%22cnhUf%22%3Anull%2C%22passaporteCodigo%22%3Anull%2C%22passaporteNumero%22%3Anull%2C%22passaportePaisCodigo%22%3Anull%2C%22passaportePais%22%3Anull%2C%22passaporteDataValidade%22%3Anull%2C%22passaporteDataEmissao%22%3Anull%2C%22fotografia%22%3A%5B%5D%2C%22situacao%22%3Anull%2C%22dataAlteracao%22%3Anull%2C%22spanSituacao%22%3Anull%2C%22motivoCancelamento%22%3Anull%2C%22spanVip%22%3Anull%2C%22vipDescricao%22%3Anull%2C%22spanProtecao%22%3Anull%2C%22protecaoDescricao%22%3Anull%2C%22motivoNaoHigienizado%22%3Anull%2C%22vivo%22%3Anull%2C%22cartoesAgregados%22%3A%5B%5D%2C%22tipoDocumento%22%3A%22CPF%22%2C%22numeroDocumento%22%3A%22$cpf%22%7D&byPassHigienizacao=false&tpPesquisa=identica");
$answer = curl_exec($ch);
if (curl_error($ch)) {
echo curl_error($ch);
}
$answer = curl_exec($ch);
$answer = trim($answer,'()');
$answer = json_decode($answer,true);
if ($lol != 1){
$cns = $answer['registro'][0]['numeroCns'];
}
curl_setopt($ch, CURLOPT_URL, 'https://cadastro.saude.gov.br/cadsusweb/restrito/consultar/visualizar.form');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
// curl_setopt($ch, CURLOPT_CAINFO, 'local/crt.crt');
curl_setopt($ch, CURLOPT_POSTFIELDS, "cns=$cns");
$answer = curl_exec($ch);
$answer = trim($answer,'()');
$answer = json_decode($answer,true);
if (curl_error($ch)) {
echo curl_error($ch);
}
$nome = $answer['nome'];
$ddd = $answer['telefone'][0]['ddd'];
$telefone = "(".$ddd.") ".$answer['telefone'][0]['numero'];
$nomemae = $answer['nomeMae'];
$cpf = $answer['cpf'];
$nomepai = $answer['nomePai'];
$sexo = $answer['sexo'];
$datanasc = $answer['dataNascimento'];
$cidade = $answer['enderecoMunicipio'];
$rua = $answer['enderecoLogradouro'];
$bairro = $answer['enderecoBairro'];
$numerocasa = $answer['enderecoNumero'];
echo '<h4>Nome:'.$nome;
echo '<h4>CNES:'.$cns;
echo '<h4>CPF:'.$cpf;
echo '<h4>Telefone:'.$telefone;
echo '<h4>Nome da Mãe:'.$nomemae;
echo '<h4>Nome do Pai:'.$nomepai;
echo '<h4>Sexo:'.$sexo;
echo '<h4>Data de Nascimento:'.$datanasc;
echo '<h4>Cidade:'.$cidade;
echo '<h4>Rua:'.$rua;
echo '<h4>Bairro:'.$bairro;
echo '<h4>N Casa:'.$numerocasa;
echo '</div>';
echo '</div>';
echo '</br>';
echo '<div class="st-panel__cont">';
echo '<div class="st-panel__content" id="output">';
?>
这种方式有效。 但是,当我做一个laravel形式,发布到与consulta.php完全相同的代码的视图时,它只是不起作用。我认为cURL没有执行。
Laravel中的:
form.blade.php
{!! Form::open(['url' => 'form', 'method' => 'POST']) !!}
@php
echo Form::label('cneslabel', 'CNES');
echo Form::text('cns');
echo Form::submit('Consultar!');
@endphp
@php
echo "</br>";
echo Form::label('cneslabel', 'CNES');
echo Form::text('cpf');
echo Form::submit('Consultar!', array('class' => 'btn'));
@endphp
{!! Form::close() !!}
web.php(路线)
Route::post('/form', function () {
return view('test');
});
view.blade.php(路线返回)
与consulta.php中的代码完全相同,只需将其复制并粘贴到视图中即可。
我错过了什么? cURL php不适用于laravel?
答案 0 :(得分:0)
在这里,您将了解有关 Laravel 处理请求的方式的详细信息:https://laravel.com/docs/5.6/requests
您必须在routes/web.php
中定义路径来处理您的Reguest:
Route::post('form', 'FormController@handle`);
FormController
内的示例方法:
public function handle(Request $request)
{
$cns = $request->cns; // instead of $_POST['cns'];
$cpf = $request->cpf; // instead of $_POST['cpf'];
// do whatever you need in here with $cns and $cpf
// if you want to see all the data on the request, you can try:
dd($request->all());
}