Browse Source

First steps to integrating with OPS API

main
Simon Bowie 3 years ago
parent
commit
01fbb9965c
4 changed files with 76 additions and 4 deletions
  1. +7
    -0
      config.env.template
  2. +52
    -0
      site/ops_api.php
  3. +15
    -2
      site/public/index.php
  4. +2
    -2
      site/random.php

+ 7
- 0
config.env.template View File

@@ -7,3 +7,10 @@ SOLR_HOSTNAME=
SOLR_PORT=
# Solr core
SOLR_CORE=

# OPS API variables
# Hostname for OPS API, usually https://ops.epo.org
OPS_URL=
# API credentials from OPS https://developers.epo.org/
CONSUMER_KEY=
CONSUMER_SECRET=

+ 52
- 0
site/ops_api.php View File

@@ -0,0 +1,52 @@
<?php

function get_access_token() {

// OPS API credentials (details at http://documents.epo.org/projects/babylon/eponet.nsf/0/F3ECDCC915C9BCD8C1258060003AA712/$File/ops_v3.2_documentation_-_version_1.3.16_en.pdf)
$ops_url = $_ENV["OPS_URL"] . '3.2/auth/accesstoken';
$auth = base64_encode($_ENV["CONSUMER_KEY"] . ":" . $_ENV["CONSUMER_SECRET"]);

// Set up API call
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ops_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Basic $auth","Content-Type: application/x-www-form-urlencoded"));
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
curl_setopt($ch, CURLOPT_POST, true);

// Give back curl result
$response = curl_exec($ch);
curl_close($ch);

// Turn the API response into useful Json
$json = json_decode($response);
$access_token = $json->access_token;

return $access_token;

}

function get_publication_details($document_reference) {

$access_token = get_access_token();

// OPS API credentials (details at http://documents.epo.org/projects/babylon/eponet.nsf/0/F3ECDCC915C9BCD8C1258060003AA712/$File/ops_v3.2_documentation_-_version_1.3.16_en.pdf)
$ops_url = $_ENV["OPS_URL"] . 'rest-services/published-data/publication/epodoc/' . $document_reference . '/biblio';

// Set up API call
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ops_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer $access_token"));

// Give back curl result
$response = curl_exec($ch);
curl_close($ch);

if (strpos($response,"No results found") === false ) {
print_r($response);
}

}

?>

+ 15
- 2
site/public/index.php View File

@@ -1,21 +1,34 @@
<?php

include '../random.php';
include '../ops_api.php';

$random_record = random_record();

?>

ID:
Document reference no.:

<?php

echo $random_record['id']
echo $random_record['doc_ref'];

?>

<br><br>

<?php

if (get_publication_details($random_record['doc_ref'])):

?>

<br><br>

<?php
endif;
?>

Application ID:

<?php

+ 2
- 2
site/random.php View File

@@ -28,8 +28,8 @@ function random_record (){
$content = $json->response->docs[$random]->content;

//Set document reference number (used for OPS API)
$output['id'] = $json->response->docs[$random]->id;;
preg_match('/=D\s(([^\s]*)\s([^\s]*)\s([^\s]*))/', $content, $doc_ref);
$output['doc_ref'] = str_replace(' ','',$doc_ref[1]);

// Search for the application ID in the content element and display it
preg_match('/Application.*\n(.*)\n/', $content, $application_id);

Loading…
Cancel
Save