MrLeche
MrLeche

Reputation: 43

Curl response unable to decode

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'=>'[email protected]');
$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

Upvotes: 0

Views: 2839

Answers (2)

MrLeche
MrLeche

Reputation: 43

Thanks your code worked with a small addition:

$result = json_decode(removeBOM($result), true);

Thanks again!

Upvotes: 1

hassan
hassan

Reputation: 8308

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>';

Upvotes: 1

Related Questions