A search interface for data from the Politics of Patents case study (part of Copim WP6): this parses data from the archive of RTF files and provides additional data from the European Patent Office API. https://patents.copim.ac.uk
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
5.9KB

  1. <?php
  2. # @name: ops_api.php
  3. # @version: 0.1
  4. # @creation_date: 2021-09-24
  5. # @license: The MIT License <https://opensource.org/licenses/MIT>
  6. # @author: Simon Bowie <ad7588@coventry.ac.uk>
  7. # @purpose: Performs functions against the European Patent Office's Open Patent Services (OPS) API
  8. # @acknowledgements:
  9. # OPS documented at https://www.epo.org/searching-for-patents/data/web-services/ops.html
  10. # OPS RESTful API specification at http://documents.epo.org/projects/babylon/eponet.nsf/0/F3ECDCC915C9BCD8C1258060003AA712/$File/ops_v3.2_documentation_-_version_1.3.18_en.pdf
  11. # OPS API functions list at https://developers.epo.org/ops-v3-2/apis
  12. function get_access_token() {
  13. // OPS API credentials (details at http://documents.epo.org/projects/babylon/eponet.nsf/0/F3ECDCC915C9BCD8C1258060003AA712/$File/ops_v3.2_documentation_-_version_1.3.18_en.pdf)
  14. $ops_url = $_ENV["OPS_URL"] . '3.2/auth/accesstoken';
  15. $auth = base64_encode($_ENV["CONSUMER_KEY"] . ":" . $_ENV["CONSUMER_SECRET"]);
  16. // Set up API call
  17. $ch = curl_init();
  18. curl_setopt($ch, CURLOPT_URL, $ops_url);
  19. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  20. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Basic $auth","Content-Type: application/x-www-form-urlencoded"));
  21. curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
  22. curl_setopt($ch, CURLOPT_POST, true);
  23. // Give back curl result
  24. $response = curl_exec($ch);
  25. curl_close($ch);
  26. // Turn the API response into useful Json
  27. $json = json_decode($response);
  28. $access_token = $json->access_token;
  29. return $access_token;
  30. }
  31. function get_publication_details($document_reference) {
  32. $access_token = get_access_token();
  33. // 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)
  34. $ops_url = $_ENV["OPS_URL"] . 'rest-services/published-data/publication/docdb/' . $document_reference . '/biblio';
  35. // Set up API call
  36. $ch = curl_init();
  37. curl_setopt($ch, CURLOPT_URL, $ops_url);
  38. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  39. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer $access_token", "Accept: application/json"));
  40. // Give back curl result
  41. $response = curl_exec($ch);
  42. curl_close($ch);
  43. if (strpos($response,"No results found") === false ) {
  44. global $output;
  45. // Turn the API response into useful Json
  46. $json = json_decode($response);
  47. // For each invention title, check if it's in the original language
  48. if (isset($json->{'ops:world-patent-data'}->{'exchange-documents'}->{'exchange-document'}->{'bibliographic-data'}->{'invention-title'})){
  49. foreach ($json->{'ops:world-patent-data'}->{'exchange-documents'}->{'exchange-document'}->{'bibliographic-data'}->{'invention-title'} as $invention_title){
  50. if ((isset($invention_title->{'@lang'})) && ($invention_title->{'@lang'} === 'ol')){
  51. $output['original_title'] = $invention_title->{'$'};
  52. }
  53. }
  54. }
  55. // For each abstract, check if it's in the original language
  56. if (isset($json->{'ops:world-patent-data'}->{'exchange-documents'}->{'exchange-document'}->{'abstract'})){
  57. foreach ($json->{'ops:world-patent-data'}->{'exchange-documents'}->{'exchange-document'}->{'abstract'} as $abstract){
  58. if ((isset($abstract->{'@lang'})) && ($abstract->{'@lang'} === 'ol')){
  59. $output['original_abstract'] = $abstract->p->{'$'};
  60. }
  61. }
  62. }
  63. return $output;
  64. }
  65. }
  66. function check_for_images($document_reference) {
  67. $access_token = get_access_token();
  68. // 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)
  69. $ops_url = $_ENV["OPS_URL"] . 'rest-services/published-data/publication/docdb/' . $document_reference . '/images';
  70. // Set up API call
  71. $ch = curl_init();
  72. curl_setopt($ch, CURLOPT_URL, $ops_url);
  73. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  74. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer $access_token"));
  75. // Give back curl result
  76. $response = curl_exec($ch);
  77. curl_close($ch);
  78. if (strpos($response,"No results found") === false ) {
  79. return $response;
  80. }
  81. }
  82. function get_images($document_reference){
  83. $access_token = get_access_token();
  84. // 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)
  85. $ops_url = $_ENV["OPS_URL"] . 'rest-services/published-data/publication/docdb/' . $document_reference . '/images';
  86. // Set up API call
  87. $ch = curl_init();
  88. curl_setopt($ch, CURLOPT_URL, $ops_url);
  89. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  90. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer $access_token"));
  91. // Give back curl result
  92. $response = curl_exec($ch);
  93. curl_close($ch);
  94. if (strpos($response,"No results found") === false ) {
  95. // Turn the API response into useful XML
  96. $xml = new SimpleXMLElement($response);
  97. // Retrieve image path from that XML
  98. $drawings_url = $_ENV["OPS_URL_IMAGES"] . '3.2/rest-services/' . $xml->xpath("///ops:document-instance[@desc='Drawing']/@link")[0][0]->__toString() . '?Range=1';
  99. // Set up API call
  100. $ch = curl_init();
  101. curl_setopt($ch, CURLOPT_URL, $drawings_url);
  102. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  103. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer $access_token","Accept: application/tiff"));
  104. // Give back curl result
  105. $response = curl_exec($ch);
  106. curl_close($ch);
  107. // Create Imagick object
  108. $image = new Imagick();
  109. // Convert image into Imagick
  110. $image->readimageblob($response);
  111. // Convert image to png
  112. $image->setImageFormat("png");
  113. // Add a subtle border
  114. $color=new ImagickPixel();
  115. $color->setColor("rgb(220,220,220)");
  116. $image->borderImage($color,1,1);
  117. // Return the Imagick object
  118. return $image;
  119. }
  120. }
  121. ?>