flattenAssocArray() public static method

Generic function to flatten an associative array into an arbitrarily delimited string.
public static flattenAssocArray ( array $array, string $format, string | null $glue = null, boolean $escape = false ) : string | array
$array array
$format string
$glue string | null
$escape boolean
return string | array If no glue provided, it won't be imploded.
Beispiel #1
0
 public function send()
 {
     //Sign the request - this just sets the Authorization header
     $this->app->getOAuthClient()->sign($this);
     // configure curl
     $ch = curl_init();
     curl_setopt_array($ch, $this->app->getConfig('curl'));
     curl_setopt($ch, CURLINFO_HEADER_OUT, true);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->getMethod());
     if (isset($this->body)) {
         curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body);
     }
     //build header array.  Don't provide glue so it'll return the array itself.
     //Maybe could be a but cleaner but nice to reuse code.
     $header_array = Helpers::flattenAssocArray($this->getHeaders(), '%s: %s');
     curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array);
     $full_uri = $this->getUrl()->getFullURL();
     //build parameter array - the only time there's a post body is with the XML,
     //only escape at this point
     $query_string = Helpers::flattenAssocArray($this->getParameters(), '%s=%s', '&', true);
     if (strlen($query_string) > 0) {
         $full_uri .= "?{$query_string}";
     }
     curl_setopt($ch, CURLOPT_URL, $full_uri);
     if ($this->method === self::METHOD_POST || $this->method === self::METHOD_PUT) {
         curl_setopt($ch, CURLOPT_POST, true);
     }
     $response = curl_exec($ch);
     $info = curl_getinfo($ch);
     if ($response === false) {
         throw new Exception('Curl error: ' . curl_error($ch));
     }
     $this->response = new Response($this, $response, $info);
     $this->response->parse();
     return $this->response;
 }
Beispiel #2
0
 /**
  * Get the Signature Base String for signing.  This is basically just all params (including the generated oauth ones)
  * ordered by key, then concatenated with the method and URL
  * GET&https%3A%2F%2Fapi.xero.com%2Fapi.xro%2F2.0%2FContacts&oauth_consumer etc.
  *
  * @return string
  */
 public function getSBS()
 {
     $oauth_params = $this->getOAuthParams();
     $request_params = $this->request->getParameters();
     $sbs_params = array_merge($request_params, $oauth_params);
     //Params need sorting so signing order is the same
     ksort($sbs_params);
     $sbs_string = Helpers::flattenAssocArray($sbs_params, '%s=%s', '&', true);
     $url = $this->request->getUrl()->getFullURL();
     //Every second thing seems to need escaping!
     return sprintf('%s&%s&%s', $this->request->getMethod(), Helpers::escape($url), Helpers::escape($sbs_string));
 }