Henry Aspden
Henry Aspden

Reputation: 1945

Rest API from PHP

I have a PayPal button which upon checkout completion redirects the user back to the homepage but in the background opens another php page which does a couple of things including updating some databases etc. However in it it I'd also like to perform a rest API request.

http://your-server/rest/createUser?username=testuser&password=testpassword

Here is the URL I need to fire and I'm not too bothered about reading the contents of this to be honest, it respond an XML file saying status OK or error messages but I don't need this... All I want to do is access the URL. This works from a browser, but when I try to use

$apicall = file_get_contents("http://your-server/rest/createUser?username=testuser&password=testpassword");

It doesn't work... Any thought ?

Upvotes: 0

Views: 48

Answers (1)

suspectus
suspectus

Reputation: 17288

For file_get_contents() to work correctly any special characters have to be encoded.

$url = 'http://your-server....';
$encodedUrl = urlencode($url);
$apicall = file_get_contents($encodedUrl);

The other thing to check is that allow_url_fopen php.ini setting is set to true. (It is by default).

Upvotes: 1

Related Questions