简体   繁体   中英

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!

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

As per Barmar's answer it's better to use json_encode, however if you must create the json by hand then you can do this

Outside of CURL

$postfields = '{"email": "'.$email1.'", "name": "'.$name1.'}';
var_dump(json_decode($postfields, true);

In CURL

CURLOPT_POSTFIELDS=>$postfields

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM