dt1888 2017-03-09 17:37
浏览 42
已采纳

卷曲响应无法解码

Not sure why my response returns as string as opposed to JSON. I'm testing an authorize.net intgration procedurally without composer on a server. Can someone help me please?

$API_LOGIN_ID = 'xxxxxxxxxxxxxxx';
$API_TRANSACTION_KEY = 'xxxxxxxxxxxxxxx';
$secure_headers = 'xxxxxxxxxxxxxxx';




$merchantAuthentication = array(
    'name' => $API_LOGIN_ID,
    'transactionKey' => $API_TRANSACTION_KEY
);



$billTo = array(
    'firstName'=>$firstName,
    'lastName'=>$lastName,
    'company'=>$company,
    'address'=>$address,
    'city'=>$city,
    'state'=>$state,
    'zip'=>$zip,
    'country'=>'USA'
);
$amount = '123.99';
$customer = array('email'=>'ferdieserrano@yahoo.com');
$transactionRequest = array(
    'transactionType' => 'authCaptureTransaction',
    'amount'=> $amount,
    'customer'=>$customer, 
    'billTo'=>$billTo
);




$hostedPaymentBillingAddressValue = '{"show": true, "required":true}';

$hostedPaymentBillingAddressOptions = array(
    'settingName'=>'hostedPaymentBillingAddressOptions',
    'settingValue'=> $hostedPaymentBillingAddressValue
);

$hostedPaymentButtonValue = '{"text" : "Pay Now"}';

$hostedPaymentButtonOptions = array(
    'settingName'=>'hostedPaymentButtonOptions',
    'settingValue'=> $hostedPaymentButtonValue
);

$hostedPaymentCustomerValue = '{"showEmail":true, "requiredEmail":true}';

$hostedPaymentCustomerOptions = array(
    'settingName'=>'hostedPaymentCustomerOptions',
    'settingValue'=> $hostedPaymentCustomerValue
);
$hostedPaymentPaymentValue = '{"cardCodeRequired" : true}';

$hostedPaymentPaymentOptions = array(
    'settingName'=>'hostedPaymentPaymentOptions',
    'settingValue'=> $hostedPaymentPaymentValue
);

$hostedPaymentReturnOptionsValue = '{"url":"https://www.example.com/continue","urlText":"Continue","cancelUrl":"https://example.com/cancel","cancelUrlText":"Cancel"}';
$hostedPaymentReturnOptions = array(
    'settingName'=>'hostedPaymentReturnOptions',
    'settingValue'=> $hostedPaymentReturnOptionsValue
);

$hostedPaymentSecurityOptionsValue = '{"captcha" : true}';
$hostedPaymentSecurityOptions = array(
    'settingName'=>'hostedPaymentSecurityOptions',
    'settingValue'=> $hostedPaymentSecurityOptionsValue
);


$setting = array($hostedPaymentBillingAddressOptions, $hostedPaymentButtonOptions, $hostedPaymentCustomerOptions, $hostedPaymentPaymentOptions, $hostedPaymentReturnOptions, $hostedPaymentSecurityOptions);
$hostedPaymentSettings = array('setting'=>$setting);
$getHostedPaymentPageRequest = array(
    'merchantAuthentication' => $merchantAuthentication,
    'transactionRequest' =>$transactionRequest,
    'hostedPaymentSettings' =>$hostedPaymentSettings
);
$options = array('getHostedPaymentPageRequest'=>$getHostedPaymentPageRequest);


$url = "https://apitest.authorize.net/xml/v1/request.api";

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($options));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Accept: application/json'
    )
); 




$result = curl_exec($ch);
curl_close($ch);
print '<pre>';
print_r(json_decode($result));
print '</pre>';
var_dump($result);

I've been playing around for a while but I'm not getting JSON. Thank you in advance

  • 写回答

2条回答 默认 最新

  • dreamMyDream2014 2017-03-09 17:46
    关注

    Replace these lines:

    curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($options));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Accept: application/json'
        )
    );
    

    with:

    $options = json_encode($options);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $options);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array (
            'Content-Type: application/json',
            'Content-Length: ' . strlen($options)
        )
    ); 
    

    Update :-

    well , after checking your code,

    i got that the error in the response it self which is returning a BOM within it ,

    which is means that you need to remove this ,

    I got a pretty good function to remove BOM from your string .

    function removeBOM($data) {
        if (0 === strpos(bin2hex($data), 'efbbbf')) {
           return substr($data, 3);
        }
    }
    

    so , your final code may be like the following :

    function removeBOM($data) {
        if (0 === strpos(bin2hex($data), 'efbbbf')) {
           return substr($data, 3);
        }
    }
    
    $result = curl_exec($ch);
    curl_close($ch);
    $result = json_decode(removeBOM($result));
    print '<pre>';
    print_r($result);
    print '</pre>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器