Pass json parameters with php

Asked

Viewed 610 times

0

I’m trying to make an integration with Paseguro that asks to send some data to a URL with the

Header = 'Accept:application/vnd.pagseguro.com.br.v3+json;charset=ISO-8859-1',
                'Content-Type: application/json;charset=ISO-8859-1'

How do I send the parameters below to a url and receive the response in php?

 {
"plan": "FFAC8AE62424AC5884C90F8DAAE2F21A",
"reference": "MEU-CODIGO",
"sender": {
"name": "José Comprador",
"email": "[email protected]",
"ip": "1.1.1.1",
"hash": "hash",
"phone": {
  "areaCode": "99",
  "number": "99999999"
},
"address": {
  "street": "Av. PagSeguro",
  "number": "9999",
  "complement": "99o andar",
  "district": "Jardim Internet",
  "city": "Cidade Exemplo",
  "state": "SP",
  "country": "BRA",
  "postalCode": "99999999"
},
"documents": [
  {
    "type": "CPF",
    "value": "99999999999"
  }]
}, 
"paymentMethod": { 
   "type": "CREDITCARD", 
    "creditCard": { 
       "token": "4C63F1BD5A0E47220F8DB2920325499D", 
     "holder": { 
        "name": "JOSÉ COMPRADOR", 
        "birthDate": "20/12/1990", 
        "documents": [ { 
           "type": "CPF", 
           "value": "99999999999" 
        } ], 
       "phone": { 
          "areaCode": "99", 
          "number": "99999999" 
        }, 
       "billingAddress": { 
          "street": "Av. PagSeguro", 
          "number": "9999", 
          "complement": "99o andar", 
          "district": "Jardim Internet", 
          "city": "Cidade Exemplo", 
          "state": "SP", 
          "country": "BRA", 
         "postalCode": 
          "99999999"
       } 
    } }}}

1 answer

1


When working with request submission, use the library Curl of php.

Example:

<?php

$json = '{
    "plan": "FFAC8AE62424AC5884C90F8DAAE2F21A",
    "reference": "MEU-CODIGO",
    "sender": {
        "name": "José Comprador",
        "email": "[email protected]",
        "ip": "1.1.1.1",
        "hash": "hash",
        "phone": {
            "areaCode": "99",
            "number": "99999999"
        },
        "documents": [{
            "type": "CPF",
            "value": "99999999999"
        }]
    },
    ...
}';

// Abre uma conexão para a URL indicada
$ch = curl_init("https://www.URL-DA-REQUISIÇÃO.com.br");

// Informa que você quer capturar o retorno
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Informa que a requisição é do tipo POST
curl_setopt($ch, CURLOPT_POST, true);

// Informa para acompanhar as requisições caso possua redirecionamento
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

// Dados que você deseja enviar. No seu caso o JSON, mas poderia ser um array com os campos
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);

//Deixe `false` caso você não possua SSL.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

// Define os headers que serão enviados
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Accept: application/vnd.pagseguro.com.br.v3+json;charset=ISO-8859-1",
    "Content-Type: application/json;charset=ISO-8859-1",
]);

// Envia a requisição e captura o resultado
$response = curl_exec($ch);

// Captura as informações da requisição (Opcional)
$info = curl_getinfo($ch);

// Captura os erros requisição, caso haja. (Opcional)
$error = curl_error($ch);

// Fecha a conexão
curl_close($ch);

// Imprimir os valores
var_dump( $response, $info, $error );

If you don’t own the library Curl installed, just run one of the codes below:

Linux (debian/Ubuntu):

  • PHP 7.0: sudo apt-get install php-curl
  • PHP 7.1: sudo apt-get install php7.1-curl
  • PHP 5.5: sudo apt-get install php5.5-curl

Windows:

If you have, however, you are disabled, just open your php.ini and add the code below.

Linux (PHP 7.1 or below):

extension=php_curl.so

Windows (PHP 7.1 or below):

extension=php_curl.dll

Linux/Windows (PHP 7.2 or higher):

extension=php_curl
  • 1

    Thanks master!! It worked perfectly.

Browser other questions tagged

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