order PyEDS within the next 10 minutes and receive PHP_EDS for free

I needed a PHP wrapper for EBSCO's EDS API and there was't one.

So I wrote one, stealing from my own Python implementation, PyEDS.

This is all really informal for now, but here's the code if anyone's interested.

<?php

class php_eds {

  private $__EDS = array();

  private function send_request($url, $post_array, $headers) {
    /* Makes a POST request via CURL for a $url with data ($post_array) and $headers.
       Based on: http://www.lornajane.net/posts/2011/posting-json-data-with-php-curl
    */
    
    $json_data = json_encode($post_array);
 
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
    $result = curl_exec($ch);
    $result = json_decode($result, true);
    
    return $result;
  }

 
  function authenticateUser($UserId, $Password) {
    /* Authenticates user with an EDS $UserId and $Password. */
    
    $req_url = "https://eds-api.ebscohost.com/authservice/rest/UIDAuth";
    $req_array = array("UserId" => $UserId,
                       "Password" => $Password,
                       "InterfaceID" => "WSapi");
    $req_headers = array("Content-Type: application/json",
                         "Accept: application/json"); //without the "Accept" header the EDS API will return XML.
    $req_results = $this->send_request($req_url, $req_array, $req_headers);
    
    $this->__EDS["AuthToken"] = $req_results["AuthToken"];
    $this->__EDS["AuthTimeout"] = $req_results["AuthTimeout"];

    return;
  }


  function openSession($Profile, $Guest, $Org) {
    /* Opens the EDS session with an EDS $Profile, the $Guest value ("y" or "n"), and the $Org nickname. */
        
    $req_url = "https://eds-api.ebscohost.com/edsapi/rest/CreateSession";
    $req_array = array("Profile" => $Profile,
                       "Guest" => $Guest,
                       "Org" => $Org);
    $req_headers =  $req_headers = array("Content-Type: application/json",
                                         "Accept: application/json",
                                         "x-authenticationToken: " . $this->__EDS["AuthToken"]);
    $req_results = $this->send_request($req_url, $req_array, $req_headers);
    
    $this->__EDS["SessionToken"] = $req_results["SessionToken"];

    return;
  }


  function closeSession() {
    /* Closes the EDS session. */
    
    $req_url = "https://eds-api.ebscohost.com/edsapi/rest/EndSession";
    $req_array = array();
    $req_headers =  $req_headers = array("Content-Type: application/json",
                                         "Accept: application/json",
                                         "x-authenticationToken: " . $this->__EDS["AuthToken"]);
    $req_results = $this->send_request($req_url, $req_array, $req_headers);
    
    return;
  }

 
  function rawSearch($query_string) {
    /* Returns search results using an ampersand-separated URL parameter string. */
    
    $url = "http://eds-api.ebscohost.com/edsapi/rest/Search?" . $query_string;
    
    $headers = array("Content-Type: application/json",
                     "Accept: application/json",
                     "x-authenticationToken: " . $this->__EDS["AuthToken"],
                     "x-sessionToken: " . $this->__EDS["SessionToken"]);
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
    $result = curl_exec($ch);
    
    return $result;
  }


  function basicSearch($query, $view="brief", $offset=1, $limit=10, $order="relevance") {
    /* Returns search results using basic arguments. */
    
    $search_json = ('{"SearchCriteria":{"Queries":[{"Term":"' . $query . '"}],"SearchMode":"bool","IncludeFacets":"n","Sort":"' . $order . '"}, "RetrievalCriteria":{"View":"' . $view . '","ResultsPerPage":' . $limit . ',"PageNumber":' . $offset . ',"Highlight":"n"},"Actions":null}');
    $req_results = $this->advancedSearch($search_json);
    
    return $req_results;
  }


  function advancedSearch($search_json) {
    /* Returns search results using the full EDS search syntax (JSON). */
    
    $req_url = "https://eds-api.ebscohost.com/edsapi/rest/Search";
    $req_array = json_decode($search_json);
    $req_headers = array("Content-Type: application/json",
                         "Accept: application/json",
                         "x-authenticationToken: " . $this->__EDS["AuthToken"],
                         "x-sessionToken: " . $this->__EDS["SessionToken"]);
    $req_results = $this->send_request($req_url, $req_array, $req_headers);
    
    return json_encode($req_results);
  }


  function retrieveRecord($accession_number, $db_code, $terms_to_highlight=null, $preferred_format="ebook-epub") {
    /* Returns metadata (including abstract and full-text if applicable) for a single record. */
 
    $req_url = "https://eds-api.ebscohost.com/edsapi/rest/Retrieve";
    $req_array = array("EbookPreferredFormat" => $preferred_format,
                       "HighlightTerms" => $terms_to_highlight,
                       "An" => $accession_number,
                       "DbId" => $db_code);
    $req_headers = array("Content-Type: application/json",
                         "Accept: application/json",
                         "x-authenticationToken: " . $this->__EDS["AuthToken"],
                         "x-sessionToken: " . $this->__EDS["SessionToken"]);
    $req_results = $this->send_request($req_url, $req_array, $req_headers);

    return json_encode($req_results);
  }


  function showInfo() {
    /* Returns EDS limiters, sort options, search tags, expanders, and search modes available to the profile. */
      
    $req_url = "https://eds-api.ebscohost.com/edsapi/rest/Info";
    $req_array = array();
    $req_headers = array("Content-Type: application/json",
                          "Accept: application/json",
                          "x-authenticationToken: " . $this->__EDS["AuthToken"],
                          "x-sessionToken: " . $this->__EDS["SessionToken"]);
    $req_results = $this->send_request($req_url, $req_array, $req_headers);
    
    return json_encode($req_results);
  }

 
  // PHP 5.4+ only.
  function prettyPrint($json_string) {
    /* Returns a pretty-printed $json_string. */
 
    return json_encode(json_decode($json_string), JSON_PRETTY_PRINT);
  }
 
}

?>
--------------

Related Content:

Leave a Comment

Your email address will not be published. Required fields are marked *


*