HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/deensekroon.komma-mediadesign.nl/wwwroot/php/Vend/Curl/CurlService.php
<?php


namespace Vend\Curl;


class CurlService
{
    private $defaultOptions;
    private $options;

    public function __construct()
    {
        // Set default options
        $this->setDefaultOptions();
    }

    /**
     * Get request
     *
     * @param $url
     * @param $token
     */
    public function get($url, $token)
    {
        $options = [
            CURLOPT_URL            => $url,

            // An array of HTTP header fields to set.
            CURLOPT_HTTPHEADER     => ['Authorization: Bearer ' . $token]
        ];

        // Merge with default options
        $this->options = $options + $this->defaultOptions;

        // Make request
        return $this->curl();
    }

    /**
     * Post request
     *
     * @param $url
     * @param $data
     * @param null $token
     * @param null $contentType
     * @return array
     */
    public function post($url, $data, $token = null, $contentType = null)
    {
        $options = [
            CURLOPT_URL            => $url,

            // The full data to post in a HTTP "POST" operation.
            CURLOPT_POSTFIELDS     => $data,
        ];


//      $payload = json_encode( array( "customer"=> $data ) );
//      curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
//      curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));


        // Not all post request require a token
        $header = [];
        if($token != null)  $header[] = 'Authorization: Bearer ' . $token;
        if($contentType != null) $header[] = 'Content-Type: ' . $contentType;

        // Add header if we have one
        if( ! empty($header))
        {
            $options = $options + [
                // An array of HTTP header fields to set.
                CURLOPT_HTTPHEADER     => $header,
            ];
        }

        // Merge with default options
        $this->options = $options + $this->defaultOptions;

        // Make request
        return $this->curl();
    }


    private function curl()
    {
        $ch = curl_init();

        // Set options
        curl_setopt_array( $ch, $this->options);

        // Response
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $response = curl_exec($ch);
        $error = curl_error($ch);

        // Close curl
        curl_close($ch);

        return [$httpCode, $response, $error];
    }


    /**
     * Set default options used in every curl request
     */
    private function setDefaultOptions()
    {
        $this->defaultOptions = [
            // TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
            CURLOPT_RETURNTRANSFER => true,

            // TRUE to include the header in the output.
            CURLOPT_HEADER         => false,

            // TRUE to follow any "Location: " header that the server sends as part of the HTTP header.
            CURLOPT_FOLLOWLOCATION => true,

            // If an empty string, "", is set, a header containing all supported encoding types is sent.
            CURLOPT_ENCODING       => "application/x-www-form-urlencoded",

            // TRUE to automatically set the Referer: field in requests where it follows a Location: redirect.
            CURLOPT_AUTOREFERER    => true,

            // The number of seconds to wait while trying to connect.
            CURLOPT_CONNECTTIMEOUT => 120,

            // The maximum number of seconds to allow cURL functions to execute.
            CURLOPT_TIMEOUT        => 120,

            // The maximum amount of HTTP redirections to follow. Use this option alongside CURLOPT_FOLLOWLOCATION.
            CURLOPT_MAXREDIRS      => 10,

            // Doesn't work with verification, so add these two parameters
            CURLOPT_SSL_VERIFYPEER => false,
            // Authenticating the certificate is not enough to be sure about the server. You typically also
            // want to ensure that the server is the server you mean to be talking to. Use CURLOPT_SSL_VERIFYHOST
            // for that. The check that the host name in the certificate is valid for the host name you're connecting
            // to is done independently of the CURLOPT_SSL_VERIFYPEER option.
            CURLOPT_SSL_VERIFYHOST => false,
        ];
    }
}