Search code examples
phpcurlphp-curl

How to pass PHP $variables in cURL array


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\": \"percy.jackson@gmail.com\",\n\"name\": \"Percy Jack\",}"

Basically replace:

  • percy.jackson2290@gmail.com with $email1
  • Percy Jack with $name1

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"]]]]]]),