0

I want to make a simple page in PHP which read from URL a specified function with relative parameters.

My URL is like www.mysite.com/Api.php?function=sendMail&name=AA000AA

I get the URL using $_SERVER['REQUEST_URI'] and pass the result to a Class i wrote which has the following construct

public function __construct($uri = '')

Everything works fine until I add the parameters with ampersand where I get the following error

Fatal error: Uncaught Error: Function name must be a string in /data/web/Api.php:36 Stack trace: #0 /data/web/Api.php(71): ApiAction->__construct('/app/Api.php?fu...')

But after a check with var_dump i got this

string(43) "/Api.php?function=sendMail&name=AA000AA"

Which tells me is a string. I've tried to cast it but it keep giving me the same error.

The class I'm calling has the following code

class ApiAction{

    protected $link;

    public function __construct($uri){
        $request = str_replace("/Api.php?", "", $uri);
        $parameters = explode("&", $request);
        $command = array();
        foreach ($parameters as $value) {
            $tmp = explode("=", $value);
            $command[$tmp[0]] = (string)$tmp[1];
        }
        $result = '';

}

But it get stuck in this error when I create a new instance of this class with

new ApiAction($_SERVER['REQUEST_URI']);

But, as mentioned before, it work perfectly before I add parameters with ampersand, I.E.

www.mysite.com/Api.php?function=sendMail

Works without any problems

4
  • here you are mistakig why you use numeric value in string function 43 string(43) "/Api.php?function=sendMail&name=AA000AA" also in name use sting I think in this string AA000AA remove 0000 pass any other string Commented Sep 18, 2017 at 10:19
  • Its not what your passing the class its how your calling it, most likely your doing $class->$method() or such. Can we see line 36 of Api.php? Commented Sep 18, 2017 at 10:21
  • I'm calling the class like this new ApiAction($_SERVER['REQUEST_URI']); Commented Sep 18, 2017 at 10:22
  • Can we see the code, the error does not match up with what your saying. Commented Sep 18, 2017 at 10:23

1 Answer 1

1

You need encode & widh %26 and use something like parse_url / parse_str to extract data.

Sign up to request clarification or add additional context in comments.

1 Comment

It worked, thanks a lot, will mark as correct answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.