我正在嘗試將 CLI curl 轉換為 php。我已經手動嘗試過這個,我已經嘗試過使用腳本,我已經嘗試過尋找解決方案,我找不到任何與使用 php 發送包含換行符的 json curl 相關的有用資訊。
curl -X POST "https://url.com" -H "accept: application/json" -H "X-API-Key: API_KEY" -H "Content-Type: application/json" -d '{"key1": "value1", "key2": "value2", "description_key": "{\n\"inner_text_key1\": \"inner_text_value\"\n}" }'
所以我有一個 curl 可以在 CLI 中正常作業,沒有問題,與上面類似。請注意,描述包含一個格式化為 json 的 STRING。這是對的。
但是,每當我嘗試在 php 中添加換行符時,整個事情都會中斷。我正在使用的 api 最終在 \n 上回傳“未轉義的字符”
我如何正確格式化,使換行符隨請求一起發送而不被解釋為未轉義的字符?
如果我從請求中洗掉換行符,它將完美發送。同樣,從 CLI 中這樣做也很完美,只是無法使其與 php 和換行符一起使用。
問題并不是真正的 php 本身,它在沒有 \n 的情況下也能作業,但 php 在下面。
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://url.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"key1\": \"value1\", \"key2\": \"value2\", \"description_key\": \"{\n\"inner_text_key1\": \"inner_text_value\"\n}\"}");
$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'X-Api-Key: no';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
echo '<pre>';
var_dump($result);
echo '</pre>';
uj5u.com熱心網友回復:
我嘗試了所有建議的方法,但沒有一個奏效。甚至沒有編碼和解碼。最后......修復很簡單。
最初我有:
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"key1\": \"value1\", \"key2\": \"value2\", \"description_key\": \"{\n\"inner_text_key1\": \"inner_text_value\"\n}\"}");
我只是把它改成
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"key1": "value1", "key2": "value2", "description_key": \"{\n\"inner_text_key1\": \"inner_text_value\"\n}"}');
所以我洗掉了“除了內部json“描述”的部分之外的所有轉義:區域并在整個事物周圍使用了單引號。突然換行作業并且所有內容都正確提交。
uj5u.com熱心網友回復:
好像用json編碼的json。
echo var_export(
json_decode(
'{"key1": "value1", "key2": "value2", "description_key": "{\n\"inner_text_key1\": \"inner_text_value\"\n}" }'
, true
), true
);
// array(
// 'key1' => 'value1',
// 'key2' => 'value2',
// 'description_key' => '{
// "inner_text_key1": "inner_text_value"
// }',
// )
JSON解碼description_key的JSON字串解碼作品。
所以你在字串的來源上遇到了問題。
如果你不能改變,那么
- json解碼字串
- json解碼
description_key偏移量 - json 再次對陣列進行編碼(一次)
- 在卷曲上使用它
編輯:
理解:陣列中的 json 是預期的。換行是問題所在。
然后你可以解碼,編碼:
$jsonString = '{"key1": "value1", "key2": "value2", "description_key": "{\n\"inner_text_key1\": \"inner_text_value\"\n}" }';
$array = json_decode($jsonString, true);
// array (
// 'key1' => 'value1',
// 'key2' => 'value2',
// 'description_key' => '{"inner_text_key1":"inner_text_value"}',
// )
if ($array['description_key']
and is_string($array['description_key'])
) {
$array['description_key'] = json_decode($array['description_key'], true);
// array (
// 'key1' => 'value1',
// 'key2' => 'value2',
// 'description_key' => array (
// 'inner_text_key1' => 'inner_text_value',
// ),
// )
// If you NEED it to be encoded in encoded json then:
$array['description_key'] = json_encode($array['description_key']);
// array (
// 'key1' => 'value1',
// 'key2' => 'value2',
// 'description_key' => '{"inner_text_key1":"inner_text_value"}',
// )
}
$jsonString = json_encode($array);
// '{"key1":"value1","key2":"value2","description_key":"{\\"inner_text_key1\\":\\"inner_text_value\\"}"}'
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/367415.html
