ThomasRedstone

Function to build API URLs

Oct 26th, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.68 KB | None | 0 0
  1.     /**
  2.      * This function provides a generic way of building a request URL for a
  3.      * range of APIs. Vendor specific elements are contained within the
  4.      * $transform and $this->apiurl variables, which should be overridden
  5.      * when this class is extended.
  6.      *
  7.      * @param array $request this array contains each of the request fields.
  8.      * @return string The returned string will be the URL, which we should make
  9.      * a request to the supplier's API using.
  10.      */
  11.     protected function buildUrl(array $request) {
  12.         /**
  13.         * $transform takes the following format:
  14.         *
  15.         * $transform = array(
  16.         *  'OPERATION-NAME'    => 'OPERATION-NAME',
  17.         *  'descriptionSearch' => 'descriptionSearch',
  18.         *  'postcode'          => 'buyerPostalCode',
  19.         *  'StartTimeFrom'     => 'itemFilter(<NUM>).name=StartTimeFrom&itemFilter(<NUM>).value',
  20.         *  'endtimeto'         => 'itemFilter(<NUM>).name=EndTimeTo&itemFilter(<NUM>).value',
  21.         *  'maxdistance'       => 'itemFilter(<NUM>).name=MaxDistance&itemFilter(<NUM>).value',
  22.         * );
  23.         *
  24.         * This allows a URL to be built without knowledge of the API being included within
  25.         * this this class, so only a different configuration need be loaded.
  26.         */
  27.         $transform = $this->transform;
  28.        
  29.         /**
  30.         * checkRequest ensures that required variables are included in the search,
  31.         * for example, eBay requires that either a category ID, or a search term
  32.         * be included in every API call.
  33.         */
  34.         \Utilities\Utilities::checkRequired(array(
  35.             $transform,
  36.             $request,
  37.             $this->apiurl,
  38.         ));
  39.        
  40.         foreach ($request as $id => $requestfield) {
  41.             if(array_key_exists($id, $transform)) {
  42.                 $id = $transform[$id];
  43.             }
  44.             $this->searchParams[$id] = $requestfield;
  45.         }
  46.         unset($id);
  47.        
  48.         $url = "{$this->apiurl}?";
  49.         $number = 0;
  50.        
  51.         /**
  52.          * $transform is the full list of possible URL fields that are acceptable
  53.          * by the API, they allow us to use friendly names up until this point,
  54.          * and at this point substitute long, search provider specific names.
  55.          */
  56.         foreach ($transform as  $id) {
  57.             if(isset($this->searchParams[$id])) {
  58.                 if($id == 'categoryId' && !is_array($this->searchParams[$id]) &&
  59.                         preg_match("/[0-9,]+/", $this->searchParams[$id])) {
  60.                     $this->searchParams[$id] = explode (',',$this->searchParams[$id]);
  61.                 }
  62.                 if($id == 'categoryId' && is_array($this->searchParams[$id])) {
  63.                     foreach($this->searchParams[$id] as $category) {
  64.                         $url .= "$id=".urlencode($category)."&";
  65.                     }
  66.                 }
  67.                 else if(!($id == 'categoryId' && !is_numeric($this->searchParams[$id]))) {
  68.                     $oldId = $id;
  69.                     /**
  70.                     * To allow API calls to use URL arrays, we use a <NUM> identifier,
  71.                     * which then gets substituted for a number, allowing any combination
  72.                     * of possible parameters to be used.
  73.                     */
  74.                     if(preg_match('#(<NUM>)#', $id)) {
  75.                         $id = preg_replace('#(<NUM>)#', $number, $id);
  76.                         $number++;
  77.                     }
  78.                     $url .= "$id=".urlencode($this->searchParams[$oldId])."&";
  79.                 }
  80.             }
  81.         }
  82.        
  83.         $url = trim($url,'&');
  84.         unset($id);
  85.         return $url;
  86.     }
Advertisement
Add Comment
Please, Sign In to add comment