簡體   English   中英

通過 PHP POST (cURL) 發送 JSON

[英]Send JSON via PHP POST (cURL)

我正在嘗試使用 cURL 發送帶有 POST 請求的 JSON 文本。 JSON 文本是:

{“頻道”:“我的頻道”,“用戶名”:“我的用戶名”,“文字”:“我的文字”}

我正在使用此代碼:

<?php

    $data = array(
        'username'    => 'My username',
        'channel'     => 'My channel'
        'text'        => 'My text',
    );                                                                    
    $data_json = json_encode($data);            

    $curl = curl_init('my-url');                                                                      
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data_json);       
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  
    curl_setopt($ch, CURLOPT_POST, 1);        
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',                                                                                
        'Content-Length: ' . strlen($data_json))                                                                       
    );                                                                                                                   

    $result = curl_exec($curl);
    echo $result ;

?>

該代碼有效,但我想在我的 JSON 文本中添加一個“附件”數組,如下所示:

{"channel": "我的頻道", "username": "我的用戶名", "text": "我的文字", "attachments": [{"text":"Line 1","color":"#000000 "},{"text":"第 2 行","color":"#ffffff"}]}

所以我嘗試將它添加到我的 $data 數組中:

'attachments' => '[{"text":"Line 1", "color":"#000000"}, {"text":"Line 2", "color":"#ffffff"}]',

但不起作用,帖子已發送,但我的“附件”被忽略......我還嘗試使用以下代碼直接從 Linux 終端發送 POST:

POST https://myurl.com
payload={"channel": "My channel", "username": "My username", "text": "My text", "attachments": [{"text":"Line 1","color":"#000000"},{"text":"Line 2","color":"#ffffff"}]}

它正在工作......我只是不明白為什么它使用手動POST而不是php/curl......

感謝您的回答。
PS:對不起我的英語不好,我是法國人...

你正在做雙 json_encode。

首先,比較這兩個:

$json1 = json_encode([
  'firstname' => 'Jon',
  'lastname' => 'Doe',
  'hp' => [
    'cc' => '+1',
    'number' => '12345678'
  ]
);
//You should get
//{"firstname":"Jon","lastname":"Doe","hp":{"cc":"+1","number":"12345678"}}

而這個:

$json2 = json_encode([
  'firstname' => 'Jon',
  'lastname' => 'Doe',
  'hp' => "['cc' => '+1', 'number' => '12345678']"
]);
//You should get
//{"firstname":"Jon","lastname":"Doe","hp":"['cc' => '+1', 'number' => '12345678']"}

你看到問題了嗎? 您將attachments鍵設置為 json 編碼字符串,因此當它發送到目標服務器時,它將被視為字符串'[{"text":"Line 1", "color":"#000000"}, {"text":"Line 2", "color":"#ffffff"}]'而不是預期的文本顏色對數組。 這里沒什么特別的,這純粹是一個粗心的錯誤:)

所以要解決這個問題,不要在attachments鍵中發送 json 編碼的字符串,而是使用這個:

$data = array(
    'username'    => 'TriiNoxYs',
    'channel'     => 'My channel'
    'text'        => 'My text',
    'attachments' => array(
        array(
            "text" => "Line 1",
            "color" => "#000000",
        ),
        array(
            "text" => "Line 2",
            "color" => "#ffffff"
        )
    )
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM