SnapchatAgent::post PHP Method

post() public method

Performs a POST request. Used for pretty much everything.
public post ( string $endpoint, array $data, array $params, boolean $multipart = FALSE ) : mixed
$endpoint string The address of the resource being requested (e.g. '/update_snaps' or '/friend').
$data array An dictionary of values to send to the API. A request token is added automatically.
$params array An array containing the parameters used to generate the request token.
$multipart boolean If TRUE, sends the request as multipart/form-data. Defaults to FALSE.
return mixed The data returned from the API (decoded if JSON). Returns FALSE if the request failed.
    public function post($endpoint, $data, $params, $multipart = FALSE)
    {
        $ch = curl_init();
        $data['req_token'] = self::hash($params[0], $params[1]);
        $data['version'] = self::VERSION;
        if (!$multipart) {
            $data = http_build_query($data);
        }
        $options = self::$CURL_OPTIONS + array(CURLOPT_POST => TRUE, CURLOPT_POSTFIELDS => $data, CURLOPT_URL => self::URL . $endpoint, CURLOPT_HTTPHEADER => array('Accept-Language: en-GB;q=1, en;q=0.9', 'Accept-Locale: en'));
        curl_setopt_array($ch, $options);
        $result = curl_exec($ch);
        // If cURL doesn't have a bundle of root certificates handy, we provide
        // ours (see http://curl.haxx.se/docs/sslcerts.html).
        if (curl_errno($ch) == 60) {
            curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/ca_bundle.crt');
            $result = curl_exec($ch);
        }
        // If the cURL request fails, return FALSE. Also check the status code
        // since the API generally won't return friendly errors.
        if ($result === FALSE || curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200) {
            curl_close($ch);
            return FALSE;
        }
        curl_close($ch);
        if ($endpoint == '/blob') {
            return $result;
        }
        // Add support for foreign characters in the JSON response.
        $result = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($result));
        $data = json_decode($result);
        return json_last_error() == JSON_ERROR_NONE ? $data : FALSE;
    }

Usage Example

コード例 #1
0
ファイル: snapchat.php プロジェクト: sadiqhirani/php-snapchat
 /**
  * Updates the current user's email address.
  *
  * @param string $email
  *   The new email address.
  *
  * @return bool
  *   TRUE if successful, FALSE otherwise.
  */
 public function updateEmail($email)
 {
     // Make sure we're logged in and have a valid access token.
     if (!$this->auth_token || !$this->username) {
         return FALSE;
     }
     $timestamp = parent::timestamp();
     $result = parent::post('/settings', array('action' => 'updateEmail', 'email' => $email, 'timestamp' => $timestamp, 'username' => $this->username), array($this->auth_token, $timestamp));
     return isset($result->param) && $result->param == $email;
 }
All Usage Examples Of SnapchatAgent::post