如何在cURL数组中传递PHP$variables



我想将一些简单的PHP变量(如$name 1和$email 1(传递给以下cURL块(PHP-Curl(

CURLOPT_POSTFIELDS =>"{n"email": "percy.jackson@gmail.com",n"name": "Percy Jack",}"

基本替换:

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

这可能吗?请帮忙!

不要尝试手动创建 JSON,创建一个数组并使用json_encode()

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

对于注释中更复杂的版本:

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

编辑版本:

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

根据Barmar的回答,最好使用json_encode,但是如果您必须手动创建json,那么您可以这样做

卷曲之外

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

在卷曲中

CURLOPT_POSTFIELDS=>$postfields
$response = trim(curl_exec($curl));
$ch = curl_init();
$post_fields = "{  n "socid": "$response",  n "date": 1492380000,  n "lines":[  n {  n "qty":"5",  n "subprice": 100,  n "fk_product": "1",  n "remise_percent": "10",  n "product_ref": "RPV"  n   n   n   n }  n ]  n }";

curl_setopt($ch, CURLOPT_URL, 'http://localhost/crm/htdocs/api/index.php/invoices');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,    $post_fields );
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Accept: application/json';

在这里,如您所见....我创建了一个名为$response的变量,在我的例子中,这个变量包含一个卷曲响应。在CURLOPT_POSTFIELD中,我传递了一个包含卷曲数据的变量。在那里你可以看到如何在curl中添加变量

最新更新