如何在 cURL 数组中传递 PHP $变量
How to pass PHP $variables in cURL array
我想将一些简单的 PHP 变量(例如 $name1 和 $email1)传递给以下 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,那么您可以这样做
CURL 之外
$postfields = '{"email": "'.$email1.'", "name": "'.$name1.'}';
var_dump(json_decode($postfields, true);
在 CURL 中
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 的变量,在我的例子中这个变量包含一个 curl 响应。在 CURLOPT_POSTFIELD 中,我传递了一个包含卷曲数据的变量。在那里你可以看到如何在 curl
中添加变量
我想将一些简单的 PHP 变量(例如 $name1 和 $email1)传递给以下 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,那么您可以这样做
CURL 之外
$postfields = '{"email": "'.$email1.'", "name": "'.$name1.'}';
var_dump(json_decode($postfields, true);
在 CURL 中
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 的变量,在我的例子中这个变量包含一个 curl 响应。在 CURLOPT_POSTFIELD 中,我传递了一个包含卷曲数据的变量。在那里你可以看到如何在 curl
中添加变量