簡體   English   中英

接收JSON格式錯誤或包含無效字符php curl時出錯

[英]error receiving JSON is badly formatted or it contains invalid character php curl

JSON格式錯誤或包含無效字符,嘗試了一些建議,但仍收到相同的錯誤,請使用以下幫助幫助我使用curl的json發布

完成卷曲要求

    $url='https://sandbox.interswitchng.com/api/v2/quickteller/payments/transfers';
     $postdata='{
            "mac":"9F4E4F53C57BE63E1F08D8F07A7BC1A9461E4A7D5304043DAA1EF54BD727B6CDE148F4FBFC5E2AD8C4A60F78DFA76304DE671FBEB70657B1628F14B6B6BAA5E1",
            "beneficiary":{
                "firstname":"chi",
                "lastname":"chi",
                "othernames":"chi"
                },
            "initiatingEntityCode":"FIB",
            "initiation":{
                "amount":"100000",
                "channel":"7",
                "currencyCode":"566",
                "paymentMethodCode":"CA"
            },
            "sender":{
                "email":"isw@interswitch.com",
                "lastname":"Phil colins",
                "othernames":"Phil colins",
                "phone":"08124888436"
            },
            "termination":{
                "accountReceivable":{
                    "accountNumber":"0012000887",
                    "accountType":"10"
                },
                "amount":"100000",
                "countryCode":"NG",
                "currencyCode":"566",
                "entityCode":"058",
                "paymentMethodCode":"AC"
            },
            "transferCode":"1016858652542545"
     }';

    $data=json_encode(json_decode($postdata));       
    $ch = curl_init($url);                                                                      
    curl_setopt($ch, CURLOPT_SSLVERSION, 6); 
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type:application/json'
    ));                                                                                                                  


    $result = curl_exec($ch); 



    echo curl_getinfo($ch) . '<br/>';
    echo curl_errno($ch) . '<br/>';
    echo curl_error($ch) . '<br/>';

    var_dump($result); 

但仍然收到錯誤JSON格式錯誤或包含無效字符。

JSON有效。

你可以做

print_r(json_decode($postdata));

它將轉換為對象。

如果只想刪除格式,並使用單行字符串:

echo json_encode(json_decode($postdata));
// output:
// {"mac":"9F4E4F53C57BE63E1F08D8F07A7BC1A9461E4A7D5304043DAA1EF54BD727B6CDE148F4FBFC5E2AD8C4A60F78DFA76304DE671FBEB70657B1628F14B6B6BAA5E1","beneficiary":{"firstname":"chi","lastname":"chi","othernames":"chi"},"initiatingEntityCode":"FIB","initiation":{"amount":"100000","channel":"7","currencyCode":"566","paymentMethodCode":"CA"},"sender":{"email":"isw@interswitch.com","lastname":"Phil colins","othernames":"Phil colins","phone":"08124888436"},"termination":{"accountReceivable":{"accountNumber":"0012000887","accountType":"10"},"amount":"100000","countryCode":"NG","currencyCode":"566","entityCode":"058","paymentMethodCode":"AC"},"transferCode":"1016858652542545"}

暫無
暫無

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

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