How to send a json via post with PHP?

Asked

Viewed 12,186 times

0

  • CURL -X POST -H "Cookie: seu_cookie=valor;" -d "{seu json}" https://www.bitcointoyou.com/Payments/, now just convert this to PHP, which has several and several posts around. The question as is does not say what has to be sent, this page is protected, only with account can access, so it is impossible to analyze, I won’t create an account just for that.

2 answers

5


First you must instantiate an array with the fields you want to send:

$array = array("cor" => "vermelho", "ano" => "2012");

Then you have to use the json_encode function to generate JSON:

$json = json_encode($array);

The call of the CURL function should look like this:

$ch = curl_init('URL_DA_CHAMADA');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($json))
);

The return handling of the call must happen in the variable that is receiving the curl_exec result, if the return is a JSON as well, you must use the json_decode function.

$jsonRet = json_decode(curl_exec($ch));

1

The page is protected for connected users, so you will most likely need to send session cookies (the name of the session, apparently, is ASP.NET_SessionId), a simplistic way is to use:

$cabeçalho = ['Cookie: ASP.NET_SessionId=' . $ValorSessao];

In addition to being a website of a exchange bitcoin must require you to use a valid UA, so also use a valid User-Agent.


Define the POST method:

CURLOPT_POST => true

Also define what to send:

CURLOPT_POSTFIELDS => $ConteudoQueQuerEnviar

Soon:

$ConteudoPOST = json_encode($sua_array);
$ConteudoCabecalho = [
    'Cookie: ASP.NET_SessionId=XXXXXXXXXXXXXXXXXXX;',
    'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'
]

Defined this use the CURL:

$curl = curl_init('https://www.bitcointoyou.com/Payments/');

curl_setopt_array($curl, [

    //-------- Segurança (caso se preocupe com isto):
    // Verifica o SSL (dificultar MITM):        
    CURLOPT_SSL_VERIFYHOST => 1,
    CURLOPT_SSL_VERIFYPEER => 2,

    // Limita o CURL para o protocolo HTTPS (dificultar SSRF e "downgrade"):
    CURLOPT_PROTOCOLS => CURLPROTO_HTTPS,

    // Limita para não seguir redirecionamento (mesmo motivo acima):
    CURLOPT_FOLLOWLOCATION => 0,
    CURLOPT_MAXREDIRS => 0,

    // Define um Tempo limita (dificultar DoS por Slow HTTP):
    CURLOPT_CONNECTTIMEOUT => 1,
    CURLOPT_TIMEOUT => 3,
    CURLOPT_LOW_SPEED_LIMIT => 750,
    CURLOPT_LOW_SPEED_TIME => 1,        
    //--------

    // Define como método POST:
    CURLOPT_POST => 1,

    // Define o JSON (o corpo do POST):
    CURLOPT_POSTFIELDS => $ConteudoPOST,

    // Define o cabeçalho:
    CURLOPT_HTTPHEADER => $ConteudoCabecalho,

    // Define para retornar o conteúdo para a variável:
    CURLOPT_RETURNTRANSFER => 1
]);

$RespostaCURL = curl_exec($curl);

Browser other questions tagged

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