Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 传递给curl的-d选项的Shell变量无法正常工作_Php_Bash_Curl - Fatal编程技术网

Php 传递给curl的-d选项的Shell变量无法正常工作

Php 传递给curl的-d选项的Shell变量无法正常工作,php,bash,curl,Php,Bash,Curl,我正在尝试使用CURL命令中的PATCH方法use-d选项。我的代码如下: 如果=”; 读行时 做 数组=($line) json_string=“{\'attributes\”:{\'member\u id\”:${array[0]},\“is\u student\”:${array[1]} 及 但是,当我使用“$json_字符串”($json_字符串用双引号括起来)时,效果很好。有什么想法吗?用双引号将变量json\u字符串括起来,以防止shell拆分单词: curl -v --fail -

我正在尝试使用CURL命令中的PATCH方法use-d选项。我的代码如下: 如果=”; 读行时 做 数组=($line)
json_string=“{\'attributes\”:{\'member\u id\”:${array[0]},\“is\u student\”:${array[1]}


但是,当我使用“$json_字符串”($json_字符串用双引号括起来)时,效果很好。有什么想法吗?

用双引号将变量
json\u字符串
括起来,以防止shell拆分单词:

curl -v --fail --silent --show-error --request PATCH -H "Content-Type:application/json"  --user "$API_KEY:$API_SECRET" -d "$json_string" "$PROFILE_URL/${array[0]}" --trace-ascii /dev/stdout
您可以通过以下方式将该行直接读取到阵列中:

#!/bin/bash

while read -a line; do
  # your logic
done < "$file"
#/bin/bash
读的时候——一行;做
#你的逻辑
完成<“$file”

阅读更多关于单词吐出的信息:

我知道当我用引号括住变量时,它会起作用。但为什么有必要呢?我在定义$json_字符串时已经使用了双引号。如果不将变量括在双引号中,shell将解释空格和通配符(如果有)。结果是,
curl
命令将把
json_string
中的单词作为单独的参数,而不是单个字符串。@sia-请查看更新的答案-我添加了一个链接,以获取有关单词吐出的更多信息。谢谢@codefrester。这种联系很有帮助。那是因为我用“”作为假设。当我将分隔符改为“|”时,代码即使没有也可以工作。但我不知道为什么我的问题被否决了
=> Send data, 2 bytes (0x2)
0000: "{
== Info: upload completely sent off: 2 out of 2 bytes
curl -v --fail --silent --show-error --request PATCH -H "Content-Type:application/json"  --user "$API_KEY:$API_SECRET" -d "$json_string" "$PROFILE_URL/${array[0]}" --trace-ascii /dev/stdout
#!/bin/bash

while read -a line; do
  # your logic
done < "$file"