pranayaryal
11/9/2016 - 8:13 PM

execute curl with post fields

execute curl with post fields

/**
     * Makes http requests to paypal's api
     * This could be done more readably with GuzzleHttp package but we dont' have time now.
     * @param string $url
     * @param array $post_fields
     * @return mixed(response from paypal)
     */
    public function executeCurl($url, $post_fields)
    {
        $this->getToken();

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_fields));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
                'Content-Type: application/json',
                'Authorization: Bearer ' . $this->access_token,
                'Content-Length: ' . strlen(json_encode($post_fields)),
            ]
        );

        $result = curl_exec($ch);
        curl_close($ch);

        $result = json_decode($result, true);
        return $result;
    }