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

问题描述 投票:-1回答:2

我想将一些简单的PHP变量(例如$ name1和$ email1)传递给以下cURL块(PHP-Curl)

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

基本替换:

这可能吗?请帮助!

php curl php-curl
2个回答
3
投票

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

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

0
投票

根据Barmar的回答,最好使用json_encode,但是,如果必须手动创建json,则可以执行此操作

CURLOPT_POSTFIELDS=>'{"email": "'.$email1.'", "name": "'.$name1.'"}';
© www.soinside.com 2019 - 2024. All rights reserved.