【发布时间】:2021-11-12 03:53:53
【问题描述】:
我有一个 cURL 请求的模板代码:通过 API 创建 1 位新患者
我需要把它转成 PHP
curl --location --request POST 'https://www.b2bmd.app/api.php?fn=patient-info' \
--form 'firstname="Test Firstname"' \
--form 'lastname="Test Lastname"' \
--form 'email="abc@gmail.com"' \
--form 'phone="(000) 000-0000"' \
--form 'address1="Address"' \
--form 'qas="{
\"q\": {
\"1\": \"Can you speak?\",
\"2\": \"Can you hear?\"
},
\"a\": {
\"1\": \"Yes\",
\"2\": \"Yes \"
}
}"' \
还有我的代码
$ch = curl_init();
$myArray = array(
array('Can you speak?','Can you hear?'),
array('Yes','Yes')
);
$json = json_encode($myArray);
/**add new **/
$url = "https://www.b2bmd.app/api.php?fn=patient-info";
$dataarr = array(
"firstname" => "John",
"lastname" => "Mike",
"email" => "John20124@yahoo.com",
"phone" => "(781) 921-4790",
"address1" => "7231 street 4",
"qas" => $json
);
$data = http_build_query($dataarr);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($ch);
if($e = curl_error($ch))
{
echo $e;
}
else
{
$decode = json_decode($resp, true);
print_r($decode);
}
curl_close($ch);
我可以全部转成php代码。
但只有 qas 是我无法转换为 php 的 JSON 样式。我尝试了很多方法,但仍然收到错误“无效的 QAS JSON 字符串”。我都试过了,还是不行。
【问题讨论】:
-
向我们展示“我尝试了一切”的含义。你试过什么?你遇到了什么错误?
-
您希望所有内容都以 json 格式发送,而不仅仅是“qas”吗?
-
这是我的错误:Array ([code] => Array ([0] => Not valid QAS JSON string.) [message] => Request Failed)
-
嗨,费尔斯。我只想要 'qas' 到 json。
标签: javascript php curl