Accessing API via php script
-
Hi Joel, Phil,
Been trying to get a listing of all of a given data point via the swagger API: /v1/point-values/{xid}/latest
Given there are several hundred entries that I need a response for I am trying to script the process but keep running into the issue of "failed to open stream: HTTP request failed! HTTP/1.1 403 Access Denied" even though a copy & paste of the string into a browser works perfectly.
The script in question is very simple:
<?php
$dpval = 2; //up to 321
$dpnum = str_pad($dpval, 16, "0", STR_PAD_LEFT);
$dpstring = "DP_HGU1_2_UII_$dpnum";
$url = "http://<ip.address>:8080/rest/v1/point-values/$dpstring/latest?useRendered=true&unitConversion=false&limit=100&useCache=true";while ($dpval <= 321) {
echo $dpstring;
echo "<br>";
echo file_get_contents($url);
echo "<br>";
echo "<br>";
$dpval++;
}
?>Have also tried numerous variations with various permutations with fopen and stream_context_create.
Cheers,
David
-
Hi David,
This is probably caused by the security token that is issued to your browser in a cookie at login. If you make a login via the API this XSRF token will be returned in the response to the successful login, with the header X-XSRF-TOKEN and should be included in all subsequent REST API requests.
Swagger is only the page at http://mango:port/swagger if swagger=true in env.properties. It's an automatically generated API explorer.
-
You can do it. Use GuzzleHttp, mess around with Swagger for a bit and be sure to take advantage of the Cookie feature.
YOU NEED TO ENABLE CORS ON YOUR MANGO INSTALLATION OR IT WILL NEVER WORK.
-
http://infiniteautomation.com/forum/topic/1944/mango-rest-api-special-key/2
Has information on enabling CORS