我如何在 php 上使用 mashape curl

How i use mashape curl on php

如何在 php 上使用 curl?

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

我知道这是错误的,但我尝试了这个(我将我的钥匙和我的公积金替换为实际值):

<?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 选项添加了一个 header,与 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;
?>