I try to get data out of the OPS system using PHP cURL functions http://php.net/manual/en/ref.curl.php. This to allow usage of oauth2 authentication.
I managed to create a token and store its data locally using the following functions:
Code: Select all
function read_token ($tokenname) {
// read token file and return token variables array
// if token not present or outdated create a new token and return new token variables array
$tokenfile="./tokens/$tokenname.dat";
$error='';
if (file_exists($tokenfile)) {
$token=unserialize(file_get_contents($tokenfile));
// convert token issued time from windows (milliseconds) format to unix (seconds) format
$tokentime=substr($token['issued_at'],0,-3);
$tokenduration=$tokentime + $token['expires_in'] - 120;
if ($tokenduration < time()) {
$error.="token '$tokenname' expired.<br>\n";
} else {
$token['error']=$error;
}
} else {
$error.="tokenfile '$tokenname' not found.<br>\n";
}
if ($error) {$token=create_token($tokenname);}
return($token);
}
function create_token ($tokenname) {
// set variables
$tokenfile="./tokens/$tokenname.dat";
$error='';
switch ($tokenname) {
case 'OPSincidental':
$ops_key='--mykey--';
$ops_secret='--mysecret--';
break;
default:
$ops_key='';
$ops_secret='';
break;
}
$tokenUrl='https://ops.epo.org/3.1/auth/accesstoken';
$tokenHeaders=array(
'Authorization: Basic '.base64_encode($ops_key.':'.$ops_secret),
'Content-Type: application/x-www-form-urlencoded'
);
$tokenPostFields='grant_type=client_credentials';
$curlOpts=array(
CURLOPT_URL => $tokenUrl,
CURLOPT_HTTPHEADER => $tokenHeaders,
CURLOPT_POSTFIELDS => $tokenPostFields,
CURLOPT_POST => 1,
CURLOPT_RETURNTRANSFER => 1
);
// obtain token
$token_request= curl_init();
curl_setopt_array($token_request, $curlOpts);
if (!$ops_token_response=curl_exec($token_request)) {$error.=curl_error($token_request)."<br>\n";}
curl_close($token_request);
// process token
$ops_token_split=explode(',', trim($ops_token_response,'{}'));
foreach ($ops_token_split as $tokenval) {
$tokenpair=explode(' : ', trim($tokenval));
$token[trim($tokenpair[0],'"')]=trim($tokenpair[1],'"');
}
// write token data to file
file_put_contents($tokenfile, serialize($token));
// add error information to token array and return result
$token['error']=$error;
return($token);
}
Code: Select all
<HTML>
<HEAD></HEAD>
<BODY>
<?php
// obtain token
include_once('./functions/token.php');
$token=read_token('OPSincidental');
if (!$token['error']) {
echo "Token:<br>\n<PRE>"; print_r($token); echo "</PRE>";
// prepare for sending data request
$error='';
$requestUrl='http://ops.epo.org/3.1/rest-services/published-data/publication/epodoc/EP1000000';
$requestHeaders=array(
'Authorization: Bearer '.$token['access_token'],
'Host: ops.epo.org',
'X-Target-URI: http://ops.epo.org',
'Accept: application/xml',
'Connection: Keep-Alive'
);
$curlOpts=array(
CURLOPT_URL => $requestUrl,
CURLOPT_HTTPHEADER => $requestHeaders,
// CURLOPT_SSL_VERIFYPEER => FALSE,
// CURLOPT_SSL_VERIFYHOST => FALSE,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER => 1
);
// send request and collect data
$ops_request= curl_init();
curl_setopt_array($ops_request, $curlOpts);
if (!$ops_response=curl_exec($ops_request)) { $error.=curl_error($ops_request)."<br>\n";}
echo "curl options:<br>\n";
echo "<PRE>";print_r($requestHeaders);echo "</PRE>";
curl_close($ops_request);
if ($error) {echo "Error:<br>\n$error";} else {echo "Result:<br>\n".htmlspecialchars($ops_response);}
} else {
echo $token['error'];
}
?>
</BODY>
</HTML>
However the result I receive is just a response header, no response data.
The header response is as follows
Code: Select all
HTTP/1.1 303 See Other
Content-Type: text/plain
Date: Mon, 20 Apr 2015 09:49:32 GMT
Location: http://ops.epo.org/3.1/rest-services/published-data/publication/epodoc/EP1000000/biblio
Server: Apache
X-API: ops-v3.1
X-EPO-Client-IP: 82.161.239.65
X-EPO-Forwarded: [82.161.239.65]
X-IndividualQuotaPerHour-Used: 335
X-RegisteredQuotaPerWeek-Used: 0
X-Throttling-Control: idle (images=green:200, inpadoc=green:60, other=green:1000, retrieval=green:200, search=green:30)
Content-Length: 0
Connection: keep-alive
I tried the following variations, all with the same result.
1)
use https;\\ with additional options:
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => FALSE
2)
simple request headers as suggested in manual (options in sample code above is taken from developers area on http://developers.epo.org):
$requestHeaders=array(
'Authorization: Bearer '.$token['access_token'],
'Accept: application/xml'
);
Does anyone have an idea what goes wrong? There is no error, a valid header is returned, just no content.
p.s. pasting the example url in a browser, does return the expected result
Best regards,
Gerben Gieling