接收JSON的错误格式错误或包含无效的字符php curl

时间:2017-07-05 12:10:37

标签: php json 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格式错误或包含无效字符。

1 个答案:

答案 0 :(得分:0)

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"}