Run a PHP with GET parameters

Asked

Viewed 54 times

-1

I would like to run a PHP file with GET parameters.

If an action is identified, a php file is accessed to capture this action.

php test.

Inside I want to run a PHP url with GET parameters

In test.php we have:

(...)


if ($status == '3'){

//FAZER ALGO

    }

(...)

If status is 3 access:

https://www.foo.com/teste2.php?nome=xxx&email=yyy

I’ve tried to do it this way:

(...)

    if ($status == '3'){

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, "https://www.foo.com/teste2.php?nome=$xxx&email=$yyy");
        curl_setopt($ch, CURLOPT_HEADER, 0);

        curl_exec($ch);

        curl_close($ch);

      }

(...)

But the URL is not executed and does not return any error message.

Is there any other way to do the same?

  • Please [Dit] and provide a [mcve] of the problem, and a better explanation of what would be "not working". A read on Stack Overflow Survival Guide in English can help a lot in the use of the site.

  • @Marcelo you are trying to redirect to the desired page or show her content?

2 answers

1


Try to use $xxx = rawurlencode($xxx); and $yyy = rawurlencode($yyy); before the curl_setopt, because if it is characters like spaces or others can complicate sending the "path"

And also add the http:// in:

curl_setopt($ch, CURLOPT_URL, "www.foo.com/teste2.php?nome=$xxx&email=$yyy");

Note: if using https:// should configure Curl, see /a/420944/3635

The code should look like this:

$xxx = rawurlencode($xxx);
$yyy = rawurlencode($yyy);

curl_setopt($ch, CURLOPT_URL, "http://www.foo.com/teste2.php?nome=$xxx&email=$yyy");

To "capture" you must use curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); and to "avoid" possible HTTP redirects use curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);, thus:

$ch = curl_init();

$xxx = rawurlencode($xxx);
$yyy = rawurlencode($yyy);

curl_setopt($ch, CURLOPT_URL, "http://www.foo.com/teste2.php?nome=$xxx&email=$yyy");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

$resposta = curl_exec($ch);

curl_close($ch);

var_dump($resposta); //lê a resposta
  • 1

    Thank you William! the error was the treatment in the "name" ($xxx)

  • @Marcelo for nothing ;)

-1

You can do this using the "file_get_contents" command. I believe it is the simplest way to make a get call in PHP.

It would look something like this

$retorno = file_get_contents(www.foo.com/teste2.php?nome=$xxx&email=$yyy);

Link to command documentation: https://www.php.net/manual/en/function.file-get-contents.php

  • Leandro, I hadn’t thought about file_get_contents. I used Guilherme’s solution anyway, thank you!

Browser other questions tagged

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