Saturday, June 4, 2022

[SOLVED] How to pass PHP $variables in cURL array

Issue

I would like to pass some simple PHP variables (such as $name1 and $email1) to the following cURL block (PHP-Curl)

CURLOPT_POSTFIELDS =>"{\n\"email\": \"[email protected]\",\n\"name\": \"Percy Jack\",}"

Basically replace:

Is this possible? Please help!


Solution

Don't try to create JSON by hand, create an array and use json_encode()

CURLOPT_POSTFIELDS => json_encode(["email" => $email1, "name" => $name1]),

For the more complex version in the comment:

CURLOPT_POSTFIELDS => json_encode([
    "sequence" => 1,
    "recipients" => [
        "signers" => [
            ["email" => $email1, "name" => $name1, "recipientId" => "1", "roleName" => "Client"]
        ],
    ]
]),

Edited version:

CURLOPT_POSTFIELDS => json_encode([
    "emailSubject" => "DocuSignAPI-CompositeTemplates",
    "emailBlurb" =>"CompositeTemplatesSample1",
    "status" => "sent",
    "compositeTemplates"=>
    ["serverTemplates"=>
     ["sequence"=>"1",
      "templateId"=>"ff32490f-58ab-4e26-a505-b32c863b2398"]],
    ["inlineTemplates"=>
     ["sequence"=>"1",
      "recipients"=>
      ["signers"=> [
       ["email" => $email1, "name" => $name1,"recipientId"=>"1","roleName"=>"Client"]]]]]]),


Answered By - Barmar
Answer Checked By - Dawn Plyler (WPSolving Volunteer)