How to pass a parameter via curl_exec to another url with PHP?

Asked

Viewed 1,564 times

0

I have a PHP that after performing an operation it calls an external URL and returns the value of this external URL, using curl_exec.

However, I need to pass a parameter to this new url, preferably as a session one. How can I pass this parameter which in this case is:

$transactionReference

Follows my code:

 echo 'codigo da transação: '.$transactionReference;      


        // chama a url de geração da nota fiscal

        // create a new cURL resource
        $ch = curl_init();

        // set URL and other appropriate options
        curl_setopt($ch, CURLOPT_URL, "http://nhaac.com/envianfe.php");
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $transactionReference); 

        // grab URL and pass it to the browser
        curl_exec($ch);

        // close cURL resource, and free up system resources
        curl_close($ch);

Just to illustrate the other PHP that this call makes is like this:

<?php


 session_start();
    require 'conexao.php';

    if($transactionReference):
         echo 'codigo do fornecedor já no nfe: '.$transactionReference;
    endif;

require_once ('client-php/lib/init.php');

echo 'codigo do fornecedor já no nfe: '.$transactionReference;
..
..
..
..
..

Maybe this is where I’m not knowing how to pick up the variable.

  • I don’t understand what you mean by "I need to pass a parameter to this new url,". What’s in $transactionReference?

  • $transactionReference is the reference code of an ID of my table that should be used there in this new URL, to return the value I want.

  • 2

    Possible duplicate of As I pass parameters through Curl?

  • @Danielomine is similar to this one. But I want to pass a parameter, preferably as a session.

  • Maybe it’s the other PHP, I’ll edit the question showing the other PHP...

  • just modify post by get... But it seems that you have a secondary doubt that is to receive the parameter. For this, just invoke $_GET['nome_variavel'] or $_POST['nome_variavel']. But there is also another issue that is going through session. If both pages are from the same domain, you can simply set up the session on the first page without the need for Curl, request, etc.. It is confused and unfocused what you ask for. If it is not duplicated it is "not clear enough"..

Show 1 more comment

1 answer

1


The response of @Daniel Omine says about the POST method and meanwhile if it’s GET it could simply do:

$parametro = http_build_query($transactionReference, '', '&', PHP_QUERY_RFC3986);

curl_setopt($ch, CURLOPT_URL, 'http://minha_url/envianfe.php?' . $parametro);

In this case assuming $transactionReference be it:

$transactionReference = ['ref' => '1', 'conteudo' => 'x'];

He’ll send the requisition as:

http://minha_url/envianfe.php?ref=1&conteudo=x

On the other page you will use:

$_GET['ref'];
$_GET['conteudo'];

For example.


I don’t understand "session preference", if you refer to session cookie and if both are the same you could use CURLOPT_HTTPHEADER or CURLOPT_COOKIE:

$cookies = http_build_query($_COOKIE, '', '; ', PHP_QUERY_RFC3986)  . ';';

curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Cookie: ' . $cookies
]); 

This would cause if the user accesses the A page, Curl would connect to the B page using the cookies that the user sent on A.


In case I just wanted to send some information I could use:

curl_setopt($ch, CURLOPT_POSTFIELDS, $transactionReference);

That would be exactly the explained in that reply. On the other page (which will receive the information) use the php://input:

$transactionReference = file_get_contents('php://input');
  • Thank you @Inkeliz, it was a matter of working both PHP. Thank you very much!

Browser other questions tagged

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