Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
2 posts Page 1 of 1
Dear Couchers,

Updated October 2021 :)

The <cms:curl /> tag can be used to make form-url or json encoded HTTP requests (including GET, POST, PUT, PATCH, and DELETE) via PHP's cURL implementation. We can use this to make HTTP requests and store the response in a defined Couch variable, which comes in handy when working with third-party APIs.

Parameters:
    - headers = HTTP headers (ex: "Authorization: Bearer <cms:show secret_token />")
    - method = optional HTTP method (ex: 'get', 'post', 'put', 'patch', or 'delete') will default to 'get'
    - data = a form-url or json encoded string depending on APIs needs
    - into = optionally name a Couch variable to store response into
    - is_json = optionally make the variable a Couch array
    - scope optionally scope the variable (using is_json will default this to 'global')

Basic usage:
Code: Select all
<cms:curl url='https://jsonplaceholder.typicode.com/posts/89' headers="Content-type: application/json" method='patch' data='{"title" : "New Title", "body" : "New body here"}' into='response' is_json='1' />

<cms:show response.title /><br />
<cms:show response.body /><br />
<cms:show response.id />

If you need to add multiple headers, simply put a pipe ('|') character between each header.

Code: Select all
<cms:curl . . . headers="Content-type: application/json | Authorization: Bearer <cms:show secret_token />" . . . />

If anyone has suggestions for improvement, please comment!

Thank you @KK for making such an extensible CMS and @trendoman for your tips 8-)

- - - - - - -

Install

Put the attachment in your addons folder and include it in your kfunctions.php file.

curl.zip
Download
(1.06 KiB) Downloaded 26 times

Code: Select all
require_once( K_COUCH_DIR.'addons/curl/curl.php' );

- - - - - - -

Examples:

Form-urlencode function (in these examples, I'm calling this simple Couch function to urlencode data when needed)

Code: Select all
<cms:func 'urlencode' string=''><cms:php>
  global $CTX;
  echo(http_build_query($CTX->get('string')));
</cms:php></cms:func>


Make a new JSON Placeholder post. (https://jsonplaceholder.typicode.com/guide/)

Code: Select all
<cms:capture into='data' is_json='1'>
  {
    "title" : "New Post"
  }
</cms:capture>

<cms:curl url='https://jsonplaceholder.typicode.com/posts' headers="Content-type: application/json" method='post' data="<cms:show data as_json='1' />" into='response' is_json='1' />

<cms:show response.title /><br />
<cms:show response.id />

- - - - -

New Post
101

Make a new Stripe customer (https://stripe.com/docs/api/customers/create)

Code: Select all
in /couch/config.php
define('STRIPE_SECRET_KEY', 'sk_test_4eC39HqLyjWDarjtT1zdp7dc');

- - - - -

<cms:capture into='data' is_json='1'>
  {
    "description" : "My First Test Customer (created for API docs)"
  }
</cms:capture>

<cms:curl url='https://api.stripe.com/v1/customers' headers="Authorization: Bearer <cms:php>echo(STRIPE_SECRET_KEY);</cms:php>" method='post' data="<cms:call 'urlencode' data />" into='response' is_json='1' />

<cms:show response.description /><br />
<cms:show response.id />

- - - - -

My First Test Customer (created for API docs)
cus_8epDebVEl8Bs2V
Good job) I PMed my "fetch-url" code for your personal use.

It is safe to add User Agent and Referrer to always know who/what is knocking.
Code: Select all
curl_setopt($ch, CURLOPT_USERAGENT, "CouchCMS ".$CTX->get('k_cms_version') );
curl_setopt($ch, CURLOPT_REFERER, $CTX->get('k_page_link') );
2 posts Page 1 of 1
cron