What is HTTP?
HTTP means HyperText Transfer Protocol. HTTP is the underlying protocol used by the World Wide Web. The protocol defines how messages are formatted and transmitted, and what actions Web servers and browsers should take in response to various commands.
For example, when you enter a URL in your browser, this actually tells Web browser make an HTTP request to the Web server.
PHPoC has an embedded web server. However, this example shows PHPoC devices to act as Web client, making HTTP request to another Web server.
Because HTTP is application protocol and operates over TCP, this example also shows how to create TCP client on PHPoC.
Hardware
- PHPoC Blue (+ USB WLAN) or PHPoC Black (+ Ethernet cable)
- Micro USB to USB Cable (to upload source code to PHPoC Device)
Quick Steps
Source code of this example is a part of PHPoC Support Packet (PSP). You need to:
- Download PHPoC Support Package.
- Upload example\net\01.php_task\http_get to PHPoC Blue/Black.
- Modify URL of webpage in task0.php
- Save the modified file.
- Configure network parameters (e.g. WiFi SSID, password, IP address ...).
Source Code
Source files includes:
- init.php: this file is run when PHPoC system is powered or reset. It is used to specify which file is run is system loop.
- task0.php: this file is run in system loop of PHPoC devices.
init.php
This file is run when PHPoC system is powered or reset. It is used to specify that task0.php is run is system loop.
PHP Code:
<?php
system("php task0.php");
?>
task0.php
[Full Code]
PHP Code:
<?php
if(_SERVER("REQUEST_METHOD"))
exit; // avoid php execution via http request
include_once "/lib/sn_dns.php";
echo "PHPoC example : get web page from www server\r\n";
$host_name = "www.google.com";
$host_addr = dns_lookup($host_name, RR_A);
if($host_addr == $host_name)
exit "$host_name : Not Found\r\n";
$tcp0_pid = pid_open("/mmap/tcp0");
echo "connect to $host_addr:80...";
pid_connect($tcp0_pid, $host_addr, 80);
for(;;)
{
$state = pid_ioctl($tcp0_pid, "get state");
if($state == TCP_CLOSED)
{
pid_close($tcp0_pid);
exit "failed\r\n";
}
if($state == TCP_CONNECTED)
break;
}
echo "connected\r\n";
$http_req = "GET / HTTP/1.1\r\n";
$http_req .= "Host: $host_name\r\n";
$http_req .= "Connection: closed\r\n";
$http_req .= "\r\n\r\n";
pid_send($tcp0_pid, $http_req);
$rbuf = "";
for(;;)
{
if(pid_recv($tcp0_pid, $rbuf) > 0)
{
echo $rbuf;
continue;
}
if(pid_ioctl($tcp0_pid, "get state") == TCP_CLOSED)
break;
}
echo "connection closed\r\n";
pid_close($tcp0_pid);
?>
[Explanation]
Source code of this file does:
- Uses DNS to translates domain names of TCP server into IP addresses.
- Opens a TCP device.
- Connects to TCP server using port 80.
- Waits until the TCP connection is established.
- Creates HTTP request message.
- Sends HTTP request message to TCP server via TCP connection.
- Reads the HTTP response message from server until TCP connection is closed by TCP server.
- Closes the TCP device.
Test and Result
- Click "Run" button on PHPoC Debugger.
- See Result on PHPoC Debugger's Console. That is content of webpage that we made request
See Also
- DNS lookup
- Sending email (SMTP)
- Sending email (ESMTP)
- MySQL - Inserting Data to Remote Server
- MySQL - Updating Data to Remote Server
- TCP Server - Single TCP Connection
- TCP Server - Multiple TCP Connections
Other Resources