专业地根据要求细分响应

时间:2018-03-28 08:26:46

标签: php json authorize.net

我不想使用SDK因为它太过分了所以我决定创建自己的SDK。我设法让它工作但我很难很好地格式化响应(使它成为一个数组或对象),所以我可以得到" resultCode"。

这是" authenticateTestRequest"的响应。我想获得" resultCode"

的价值
{
    "messages": {
        "resultCode": "Ok",
        "message": [
            {
                "code": "I00001",
                "text": "Successful."
            }
        ]
    }
}

没有自定义漂亮显示的响应

string(86) "{"messages":{"resultCode":"Ok","message":[{"code":"I00001","text":"Successful."}]}}"

json_decode和JSON Pretty Print无效

我的代码(如果我放了json_decode则打印NULL):

function sc_test_authenticate( $api_endpoint, $login, $tran_key ) {     

    $params = array(
        'authenticateTestRequest' => array(
            'merchantAuthentication' => array(
                'name' => $login,
                'transactionKey' => $tran_key
            )
        )
    );

    $curl = curl_init();    

    curl_setopt($curl, CURLOPT_URL, $api_endpoint);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 300);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);  

    $response = curl_exec($curl);       

    curl_close($curl);  

    return json_decode($response);      
}

0 个答案:

没有答案