PHP code example of cct-marketing / rest

1. Go to this page and download the library: Download cct-marketing/rest library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cct-marketing / rest example snippets


use CCT\Component\Rest\Config;
use CCT\Component\Rest\Http\Definition\RequestHeaders;
use CCT\Component\Rest\Http\Request;
use CCT\Component\Rest\Serializer\Context\Context;

class MyRequest extends Request
{
    protected function setUp()
    {
        $this->config->set(Config::URI_PREFIX, '/test/');
    }

    public function apiCall(QueryParams $queryParams = null)
    {
        $this->config->set('serialization_context', Context::create()->setGroups(['read']));

        $headers = RequestHeaders::create(
            [
                'Accept' => 'application/json',
                'X-Requested-With' => 'XMLHttpRequest',
            ]
        );

        $this->setHeaders($headers);

        return parent::requestGet($this->getUri(), $queryParams);
    }
}

use CCT\Component\Rest\AbstractClient;
use CCT\Component\Rest\Config;

class RESTClient extends AbstractClient
{
    /**
     * @return ScrapeRequest
     */
    public function myAPI(): MyRequest
    {
        $config = clone $this->config;
        $modelClass = TestModel::class;

        $serializer = $this->getBuiltSerializer($config);
        if ($this->shouldUseDefaultResponseTransformers() && null !== $serializer) {
            $this->applyDefaultResponseTransformers($config, $serializer, $modelClass);
        }

        return $this->createRequestInstance(TestRequest::class, $config, null);
    }
}

$config = new \CCT\Component\Rest\Config([
    \CCT\Component\Rest\Config::ENDPOINT => 'https://api.endpoint.com/',
    \CCT\Component\Rest\Config::DEBUG => true,
]);

/**
 * Send Request
 */
$client = new Client($config);
$query = new QueryParams();
$query->set('message', 'hello');
$response = $client->myAPI()->apiCall($query);

var_dump($response->getData());