我如何在 php 上使用 mashape curl



我如何在带有curl的php上使用它?

curl --get --include 'https://consulta-situacao-cpf-cnpj.p.mashape.com/consultaSituacaoCNPJ?cnpj={cnpj}' 
  -H 'X-Mashape-Key: MYKEY'

我知道这是错误的,但我尝试这样做(我将我的密钥和我的 CPF 替换为实际值):

<?php
$cpf = "MY CPF";
$url = "https://consulta-situacao-cpf-cnpj.p.mashape.com/consultaSituacaoCPF?cpf=".$cpf;
$data = array('X-Mashape-Key' => 'MY KEY'); //My Key
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$name = trim(curl_exec($curl));
curl_close($curl);
echo $name;
?>

如果您查看手册页curl,您会发现 -H 选项添加了一个标题,与 POST 数据无关。要在 PHP 中执行相同的操作,请设置 CURLOPT_HTTPHEADER 选项:

<?php
$cpf = "MY CPF";
$url = "https://consulta-situacao-cpf-cnpj.p.mashape.com/consultaSituacaoCPF?cpf=".$cpf;
$headers = array("X-Mashape-Key: MY KEY"); //My Key
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$name = trim(curl_exec($curl));
curl_close($curl);
echo $name;
?>

相关内容

  • 没有找到相关文章

最新更新