Reputation: 1338
I am trying to set a simple client-server system for a mobile app. Server side is written in PHP. This server should handle requests from the app (a client) such as exchanging data in the form of json objects.
So i need my server to listen to requests and respond.
All the examples i found include the 'Socket' functions (socket_create(), socket_bind() etc.), and somewhere i need to specify the port i'm listening on.
My question is - the app sends the request to some url - http://example.com (or something like that).
Obviously in the client code i am not specifying the port i am sending the request to, so do i need to listen on all ports? How can i achieve this?
Upvotes: 0
Views: 539
Reputation: 4554
Usually PHP is used in combination with a web server (for example Apache or nginx), which handles the low-level socket communication, so that you can concentrate only on the business logic of your system.
This way, your server code can be as simple as:
<?php
echo json_encode(array(
'status' => 'OK',
'user' => array(
'id' => 6,
'username' => 'foo',
),
));
FYI: Web servers by default use port 80 for the http protocol and port 443 for https. So, when the port number is not specified, then the default values are used. For example http://example.com
is the same as http://example.com:80
.
Upvotes: 1