|                                                                                                                                          | 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 | <?php
# @name: ops_api.php
# @version: 0.1
# @creation_date: 2021-09-24
# @license: The MIT License <https://opensource.org/licenses/MIT>
# @author: Simon Bowie <ad7588@coventry.ac.uk>
# @purpose: Performs functions against the European Patent Office's Open Patent Services (OPS) API
# @acknowledgements:
# OPS documented at https://www.epo.org/searching-for-patents/data/web-services/ops.html
# OPS RESTful API specification at http://documents.epo.org/projects/babylon/eponet.nsf/0/F3ECDCC915C9BCD8C1258060003AA712/$File/ops_v3.2_documentation_-_version_1.3.16_en.pdf
# OPS API functions list at https://developers.epo.org/ops-v3-2/apis
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", "Accept: application/json"));
  // Give back curl result
  $response = curl_exec($ch);
  curl_close($ch);
  if (strpos($response,"No results found") === false ) {
    // Turn the API response into useful Json
    $json = json_decode($response);
    // For each invention title, check if it's in the original language
    foreach ($json->{'ops:world-patent-data'}->{'exchange-documents'}->{'exchange-document'}->{'bibliographic-data'}->{'invention-title'} as $invention_title){
      if ($invention_title->{'@lang'} === 'ol'){
        $output['original_title'] = $invention_title->{'$'};
      }
    }
    // For each abstract, check if it's in the original language
    foreach ($json->{'ops:world-patent-data'}->{'exchange-documents'}->{'exchange-document'}->{'abstract'} as $abstract){
      if ($abstract->{'@lang'} === 'ol'){
        $output['original_abstract'] = $abstract->p->{'$'};
      }
    }
    return $output;
  }
}
function check_for_images($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 . '/images';
  // 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 ) {
    return $response;
  }
}
function get_images($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 . '/images';
  // 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 ) {
		// Turn the API response into useful XML
		$xml = new SimpleXMLElement($response);
    // Retrieve image path from that XML
    $drawings_url = $_ENV["OPS_URL_IMAGES"] . '3.2/rest-services/' . $xml->xpath("///ops:document-instance[@desc='Drawing']/@link")[0][0]->__toString() . '?Range=1';
    // Set up API call
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $drawings_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer $access_token","Accept: application/pdf"));
    // Give back curl result
    $response = curl_exec($ch);
    curl_close($ch);
    // Return the PDF response
    return $response;
  }
}
?>
 |