我已经通过bash脚本连接到API,如下所示:
#!/bin/bash
curl "https://example.com/templates/search?field=template_id& field=name" \
-H "Authorization: Bearer ----------------------------------------------------------------"
curl -X POST "https://example.com/audits" \
-d '{ "template_id": "template_28c5b7ec77f34ea7881b6a9ef9c01b91", "header_items": [ { "item_id": "f3245d40-ea77-11e1-aff1-0800200c9a66", "label": "Audit Title", "type": "textsingle", "responses": { "text": "${title}" } }, { "item_id": "f3245d43-ea77-11e1-aff1-0800200c9a66", "label": "Conducted By", "type": "textsingle", "responses": { "text": "John Citizen" } } ] }' \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ----------------------------------------------------------------"
这成功连接。
然后,我有了一个PHP网页,其中包含一个表单,您可以在其中输入审核的标题。当我在表单上按保存时,以上脚本将运行并创建审核。但是,标题是$ {title}而不是文本框的值。
甚至可以通过这种方式将变量值发布到bash脚本中吗?
更新了评论和聊天讨论。
PHP代码将shell脚本作为PHP变量运行在sh ./curl.sh $title
哪里$title
(理想情况下,它应该正确地引用$title
数据以免混淆shell)。因此,shell脚本需要从其命令行参数中选择标题$*
,而不是从shell变量中选择标题$title
。
发送的JSON文档用单引号引起来,这意味着$title
外壳将看不到变量扩展。
您需要暂时突破单引号引起的字符串,以使变量扩展:
-d '{ "template_id": "template...{ "text": "'"$*"'" }...
外部的双引号"'"$*"'"
属于JSON文档,单引号内的内容属于shell。内部双引号用引号引起来$*
(如果它包含空格和/或文件名通配符模式)。
PHP代码中还有一个附加问题,这意味着$title
从未正确设置PHP变量。解决此问题后,它可以按预期工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句