From 25a8483580b6b12866b4619e5622431d77d57112 Tue, 26 Jul 2011 15:38:08 +0100
From: jeremyfrench <jeremy@jeremyfrench.co.uk>
Date: Tue, 26 Jul 2011 15:37:20 +0100
Subject: [PATCH] 1223352 JeremyFrench implement people-search in linkedin module.

diff --git a/linkedin.inc b/linkedin.inc
index 843031d..60b4a6e 100644
--- a/linkedin.inc
+++ b/linkedin.inc
@@ -496,3 +496,103 @@
   }
   return $fields;
 }
+
+
+/**
+ * Search for people on linkedin
+ * @see https://developer.linkedin.com/documents/people-search-api
+ * usage:
+ * @param array $search_values and array of key value pairs to search on.
+ * @param integer $start where to start the search results default = 0.
+ * @param integer $count how many results to return max/default 25.
+ * @param array $fields an array of fields to return.
+ * @param array $facets facets to filter by.
+ * @param string $sort sort by 'connections','recommenders','distance','relevance'
+ * @return an array of parsed xml as specified by the likedin api
+ */
+function linkedin_search_people($search_values, $start = 0, $count = 25, $fields = NULL,  $facets = NULL, $sort= NULL) {
+  $url = "http://api.linkedin.com/v1/people-search";
+
+  // Add fields to the URL.
+  if (isset($fields)) {
+    // TODO: non people facets
+    $url .= ':(people' . _linkedin_build_fields_request($fields) . ')';
+  }
+  $url .= '?';
+
+  // Add search values to the URL.
+  $valid_search_values = array('first-name',
+                               'last-name',
+                               'company-name',
+                               'current-company',
+                               'title',
+                               'current-title',
+                               'school-name',
+                               'current-school',
+                               'country-code',
+                               'postal-code',
+                               'distance',
+  );
+  $search_array = array();
+  foreach ($search_values as $search_key => $search_value) {
+    if (in_array($search_key, $valid_search_values)) {
+      $search_array[] = $search_key . '=' . urlencode($search_value);
+    }
+  }
+  $url .=  implode('&', $search_array);
+
+  $url .= '&start=' . $start;
+  $url .= '&count=' . $count;
+
+  // Add facets.
+  $valid_facets = array('location',
+                        'industry',
+                        'network',
+                        'language',
+                        'current-company',
+                        'past-company',
+                        'school'
+                        );
+  // TODO: field selectors may be available via facests
+  $facet_array = array();
+  $included_facets = array();
+  foreach ($facets as $facet_key => $facet_value) {
+    if (in_array($facet_key, $valid_facets)) {
+      if (isset($facet_value)) {
+        $facets_array[] =  'facet=' . $facet_key . ',' . urlencode($facet_value);
+      }
+      $included_facets[]  = $facet_key;
+    }
+  }
+  if (!empty($included_facets)) {
+    $url .= '&facets=' . implode(',', $included_facets);
+    if (!empty($facets_array)) {
+      $url .= '&' . implode('&', $facets_array);
+    }
+  }
+
+  // Add sort.
+  $valid_sort_values = array('connections',
+                             'recommenders',
+                             'distance',
+                             'relevance',
+                             );
+  if (isset($sort) && array_search($sort, $valid_sort_values)) {
+    $url .= '&sort=' . $sort;
+  }
+
+  global $user;
+  $signature = new OAuthSignatureMethod_HMAC_SHA1();
+  $consumer_key = variable_get('linkedin_consumer_key', '');
+  $consumer_secret = variable_get('linkedin_consumer_secret', '');
+  $consumer = new OAuthConsumer($consumer_key, $consumer_secret, NULL);
+  $row = db_fetch_array(db_query("SELECT * FROM {linkedin_token} WHERE uid = %d AND type = 'access'", $user->uid));
+  $token = new OAuthConsumer($row['token_key'], $row['token_secret'], 1);
+  $request = OAuthRequest::from_consumer_and_token($consumer, $token, "GET", $url);
+  $request->sign_request($signature, $consumer, $token);
+  $header = $request->to_header("https://api.linkedin.com");
+
+  $results = _linkedin_http_request($url, $header, $body = NULL);
+
+  return  _linkedin_parse_fields($results);
+}
\ No newline at end of file
