Curl passing and receiving parameters?

Asked

Viewed 383 times

0

I have the link $page below:

$pagina = 'http://www.google.com';

I’d like to know the difference between:

$ch = curl_init($$pagina);

and:

$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $pagina );

What’s the difference?

It’s just that I need send a few information to the API of CIELO and after being processed those information, receive of himself to reply.

I also need to send a variable for header of that requisition.

I’d like an example by doing you a favor!

I’m doing as below, but only gives error not receiving the variable:

{"message":"Erro: token x-picpay-token obrigatório.","code":"401"}

The staff of Picpay said it is mandatory to send x-picpay-token in the header but I don’t think I’m doing the right thing

Code:

<?php

require_once "_global/_erros/erros.ini";

$site     = "http://www.hotplateprensas.com.br";

$producao = "https://appws.picpay.com/ecommerce/public/payments";

$headers  = array ( 
      "Content-Type   : application/json",
      "x-picpay-token : a840f8a9-e9b1-4dd5-a1c4-6ed32209d505"
);

$dados = array ( 
           "referenceId    : " . 1234,
           "callbackUrl    : " . $site . "/picpay.php",
           "returnUrl      : " . $site . "/picpayReturn.php",
           "value          : " . 222.22,
           "buyer          : " . array (
                "firstName : " . "Carlos",
                "lastName  : " . "Alberto",
                "document  : " . 12345678900,
                "email     : [email protected]",
                "phone     : +55 32 3721-6149" //  +55 27 12345-6789
           ),
);

$ch = curl_init($producao);

curl_setopt( $ch, CURLOPT_URL, $producao );
curl_setopt( $ch, CURLOPT_POST,  true );
curl_setopt( $ch, CURLOPT_POSTFIELDS,  json_encode ( $dados ) );
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_HEADER, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLINFO_HEADER_OUT, true );

$result = curl_exec( $ch );
$erro   = curl_error( $ch );
$info   = curl_getinfo( $ch );

print "<pre>";
    print_r( $result );
    print_r( $erro );
    //print_r( $info );
print "</pre>";

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

?>

After the guidance of the colleague and after changing, now I have 2 problems:

<?php

$site     = "http://www.hotplateprensas.com.br";

$producao = "https://appws.picpay.com/ecommerce/public/payments";

$headers  = array ( 
      "Content-Type   : application/json",
      "x-picpay-token : a840f8a9-e9b1-4dd5-a1c4-6ed32209d505"
);

$dados = array ( 
           "referenceId    : " . 1234,
           "callbackUrl    : " . $site . "/picpay.php",
           "returnUrl      : " . $site . "/picpayReturn.php",
           "value          : " . 222.22,
           "buyer          : " . array (
                "firstName : " . "Carlos",
                "lastName  : " . "Alberto",
                "document  : " . 12345678900,
                "email     : [email protected]",
                "phone     : +55 32 3721-6149" //  +55 27 12345-6789
           ),
);

$ch = curl_init($producao);

curl_setopt( $ch, CURLOPT_URL, $producao );
curl_setopt( $ch, CURLOPT_POST,  true );
curl_setopt( $ch, CURLOPT_POSTFIELDS,  json_encode ( $dados ) );
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_HEADER, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLINFO_HEADER_OUT, true );

$result = curl_exec( $ch );
$erro   = curl_error( $ch );
$info   = curl_getinfo( $ch );

print "<pre>";
    print_r( $result );
    print_r( $erro );
    //print_r( $info );
print "</pre>";

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

?>

Along those lines,

 "phone     : +55 32 3721-6149" //  +55 27 12345-6789

Are you saying:

Notice: Array to string conversion in D:\Trabalhos\host\htdocs\hotplateprensas.com.br\picpay2.php on line 34

and continues to say:

HTTP/1.1 401 Unauthorized
Date: Mon, 18 Feb 2019 23:12:11 GMT
Content-Type: application/json;charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Content-Encoding: gzip
Cache-Control: no-cache, private
Vary: Accept-Encoding
X-Powered-By: PHP/7.2.8

    {"message":"Erro: x-picpay-token=': a840f8a9-e9b1-4dd5-a1c4-6ed32209d505' inválido.","code":"401"}
  • Instead of using an associative array, try doing $headers = array ( "Content-Type: application/json",&#xA; "x-picpay-token: a840f8a9-a8d9-4dd5-a1c3-6ed32209d505"&#xA;)

  • Ué. But this is how it is in the question. No?

  • No, you are using an associative array, not an array of multiple strings, as the documentation says. http://php.net/manual/en/function.curl-setopt.php

  • understood. But, please, after making the changes, there were other problems that I added at the end of the question

  • Apparently the picpay token is incorrect, since the Cielo system could not validate it. On the error on line 34, the point is that you are trying to concatenate an array with a string, which is not possible. What you can do in this case is print this array as a json, using the function json_encode

  • Now, you’re fast, I was here setting up the array thing that I noticed and reenacting the correct shape and you already managed to kill the riddle. But next, you keep complaining about not getting the token there at Picpay. It’s not at CIELO. That’s not the bandstand. But I already tested with the bandstand and it gives the same error, The Picpay people said they needed to send the token in the request header. Guess that’s what I’m doing wrong.

  • Strange... It seems to be a problem with picpay... I tried the example on the doc of their site and even the test user of the API gave error. Your requisition is right, he’s passing the header straight. I honestly don’t know what the problem might be, maybe it is the case to wait another collect or contact Picpay support to see what the problem may be.

  • um, here, I did another test here and gave array error again. I put right this time without variable, "phone : +55 32 3721-6149" // +55 27 12345-6789 ), );

  • http://hotplateprensas.com.br/picpay.php

  • Well, the array error thing I found now only missing the authentication error

Show 5 more comments
No answers

Browser other questions tagged

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