0

I want to make a small PHP script that will check a web app periodically. The communication will be only in json. The code will run on Google App Engine.

First request will be an HTTP POST request with form data like this: username=user&password=pass

POST http://www.example.com/login HTTP/1.1 

If login fails the response json will be:

{
    "message": "Failed login"
}

If successful:

{
    "response": "OK",
    "username": "user",
    "protocol": "http"
}

Subsequent requests will be GET, POST and PUT requests containing json and also the response will be in json.

This code will be run by Google App Engine's Cron for PHP ones a day.

Since I have little knowledge of PHP, I would like to know how I should implement this.

  • How do I make the http request to the web app.
  • How do I remember login/authentication headers from one request to another.
  • How do I handle reading, writing and modifying of json in PHP.

All I need is a basic example and guidelines to get me started.

Thank you in advance.

Tase

Tase
  • 35
  • 5

2 Answers2

1

Just to get you started: cURL and json_decode/json_encode.

If all of your requests will be in the same script, run all at the same time, you'll just need to use the cookies and headers options for cURL.

I'll leave a full example to someone else, if necessary.

Also, you should use HTTPS if possible. It's never a good idea to send username/password in clear text, no matter how trivial the service.

Edit: as it looks like GAE doesn't support curl (thanks @boombatower), check this out: Replacing CURL with urlfetch in PHP

Community
  • 1
  • 1
Jason
  • 13,606
  • 2
  • 29
  • 40
1

Given that GAE does not currently support cURL I would recommend an approach like How do I send a POST request with PHP?. Down the road you may be able to use something slick like Guzzle (if GAE adds cURL support).

Use json_decode/json_encode to create JSON to send and parse the responses.

Community
  • 1
  • 1
boombatower
  • 651
  • 4
  • 6