forked from UoEMainLibrary/ASRestAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnectToAS.php
58 lines (52 loc) · 1.38 KB
/
ConnectToAS.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
/**
* Created by JetBrains PhpStorm.
* User: cknowles
* Date: 09/01/2014
* Time: 14:38
* To change this template use File | Settings | File Templates.
*/
$resp = httpRequest("localhost",
8089, "POST", "/locations",
array("firstName" => "John", "lastName" => "Doe"));
function httpRequest($host, $port, $method, $path, $params) {
// Params are a map from names to values
$paramStr = "";
foreach ($params as $name, $val) {
$paramStr .= $name . "=";
$paramStr .= urlencode($val);
$paramStr .= "&";
}
// Assign defaults to $method and $port, if needed
if (empty($method)) {
$method = 'GET';
}
$method = strtoupper($method);
if (empty($port)) {
$port = 80; // Default HTTP port
}
// Create the connection
$sock = fsockopen($host, $port);
if ($method == "GET") {
$path .= "?" . $paramStr;
}
fputs($sock, "$method $path HTTP/1.1\r\n");
fputs($sock, "Host: $host\r\n");
fputs($sock, "Content-type: " .
"application/x-www-form-urlencoded\r\n");
if ($method == "POST") {
fputs($sock, "Content-length: " .
strlen($paramStr) . "\r\n");
}
fputs($sock, "Connection: close\r\n\r\n");
if ($method == "POST") {
fputs($sock, $paramStr);
}
// Buffer the result
$result = "";
while (!feof($sock)) {
$result .= fgets($sock,1024);
}
fclose($sock);
return $result;
}