PHP cURL demo
This commit is contained in:
@@ -11,4 +11,5 @@ $routes->get('/provision/prepare', 'Provision::prepareProvision');
|
|||||||
$routes->get('/provision/update', 'Provision::updateProvision');
|
$routes->get('/provision/update', 'Provision::updateProvision');
|
||||||
$routes->get('/provision/release', 'Provision::releaseProvision');
|
$routes->get('/provision/release', 'Provision::releaseProvision');
|
||||||
$routes->get('/provision/target', 'Provision::directProvision');
|
$routes->get('/provision/target', 'Provision::directProvision');
|
||||||
|
$routes->get('/provision/demo', 'Provision::demo');
|
||||||
|
|
||||||
|
|||||||
@@ -319,4 +319,64 @@ class Provision extends BaseController
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function demo():string {
|
||||||
|
|
||||||
|
$result = "Unhandled exception";
|
||||||
|
$base_url = "http://172.16.4.90:3000";
|
||||||
|
$cookies = tempnam('/tmp','cookie.txt');
|
||||||
|
|
||||||
|
# 1. Login
|
||||||
|
$url = $base_url . "/api/auth/login";
|
||||||
|
$ch = curl_init( $url );
|
||||||
|
# Setup request to send json via POST.
|
||||||
|
$payload = json_encode( array( "auth"=> "admin", "password" => "may12002" ) );
|
||||||
|
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
|
||||||
|
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
|
||||||
|
# Return response instead of printing.
|
||||||
|
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
|
||||||
|
# Fetch the headers, not the body content:
|
||||||
|
//curl_setopt($ch, CURLOPT_HEADER, true); // we want headers
|
||||||
|
//curl_setopt($ch, CURLOPT_NOBODY, true); // we don't need body
|
||||||
|
# Cookie jar
|
||||||
|
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies);
|
||||||
|
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies);
|
||||||
|
|
||||||
|
# Send request.
|
||||||
|
$result = curl_exec($ch);
|
||||||
|
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
|
$result = 'HTTP code: ' . $httpcode;
|
||||||
|
|
||||||
|
curl_close($ch);
|
||||||
|
|
||||||
|
if ($httpcode != "204") {
|
||||||
|
return "Login failed! $result";
|
||||||
|
}
|
||||||
|
|
||||||
|
# 2. Get token
|
||||||
|
$url = $base_url . "/api/user/tokens";
|
||||||
|
$ch = curl_init( $url );
|
||||||
|
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
|
||||||
|
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies);
|
||||||
|
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies);
|
||||||
|
$result = curl_exec($ch);
|
||||||
|
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
|
curl_close($ch);
|
||||||
|
|
||||||
|
$token = null;
|
||||||
|
$arr = json_decode($result, true);
|
||||||
|
foreach ($arr as $item) {
|
||||||
|
if ($item["expired"]===false) {
|
||||||
|
$token = $item["id"];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
$result = "Found token: $token";
|
||||||
|
|
||||||
|
# Print response.
|
||||||
|
return "<pre>$result</pre>";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user