0

So I'm working on expedia's API, and have successfully worked many of the API requests but am unable to get just one to work though I've tried and searched in vain for what could be the problem.

My Function looks like this:

global $cid, $apiKey, $sig, $minor_rev;


$url= 'http://api.ean.com/ean-services/rs/hotel/v3/paymentInfo?minorRev='.$minor_rev.'';
$url .= '&cid='.$cid.'';
$url .= '&sig='.$sig.'';
$url .= '&apiKey='.$apiKey.'';
$url .= '&customerUserAgent='.$PaymentTypeData['customerUserAgent'].'';
$url .= '&customerIpAddress='.$PaymentTypeData['customerIpAddress'].'';
$url .= '&customerSessionId='.$PaymentTypeData['customerSessionId'].'';
$url .= '&locale=en_US&_type=json';
$url .= '&currencyCode='.$PaymentTypeData['currencyCode'].'';
$url .= '&hotelId='.$PaymentTypeData['hotelId'].'';
$url .= '&supplierType=E';
$url .= '&rateType=MerchantStandard';



$header[] = "Accept: application/json";
$header[] = "Accept-Encoding: gzip, deflate";
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTPHEADER, $header );
curl_setopt($ch,CURLOPT_ENCODING , "gzip");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$result = json_decode(curl_exec($ch), true);


$HotelPaymentResponse = array();
$HotelPaymentResponse[] = $result['HotelPaymentResponse'];


echo '<pre>';
echo $url;
print_r($result);
echo '<h1>GET HOTEL PAYMENT TYPES</h1>';
print_r($HotelPaymentResponse);
echo '</pre>';

return $HotelPaymentResponse;

When I echo the URL and try it in the browser the result is as expected, an array of the payment types in json form.... but for the life of me getting this into the php array for the return eludes me.

Here is a link to the API spec: http://developer.ean.com/docs/payment-types/

And yes I have checked out JSON guides often posted on S.Overflow....

any help would be so very appreciated...

PS:

the API says the repose will look like:

{
"HotelPaymentResponse": {
    "@size": "4",
    "@currencyCode": "USD",
    "customerSessionId": "0ABAAAC9-9A32-7914-9E32-D7EC7F906769",
    "PaymentType": [
        {
            "code": "AX",
            "name": "American Express"
        },
        {
            "code": "DS",
            "name": "Discover"
        },
        {
            "code": "CA",
            "name": "Master Card"
        },
        {
            "code": "VI",
            "name": "Visa"
        }
    ]
}
}

BUT my Array is returning:

Array
(
    [0] => 
)

SOLVED:

Thanks to working through the comments below and researching the error thrown, 505 - I googled and found someone say they had an error because of a stray space in the url. My error was because my Customer user agent was populating from php's user HTTP agent... Removed that and manually entered and now I can curl the response.

11
  • Does it throws an error? Commented Apr 8, 2016 at 16:39
  • No error being thrown, but the array is printing Array ( [0] => ) Commented Apr 8, 2016 at 16:41
  • run a var_dump() on the $result and see what it shows. If it's NULL, then your array will be empty, and there's an issue in your API call Commented Apr 8, 2016 at 16:53
  • Yeah it is NULL, but what I don't understand is that when I visit the echoed URL it shows up fine in the browser - any ideas? Commented Apr 8, 2016 at 16:55
  • This specific bit: echo $url; print_r($result); What does that show? Commented Apr 8, 2016 at 16:59

1 Answer 1

1

When you use true as the second argument to json_decode, JSON objects are decoded to PHP associative arrays, not objects. So $result->HotelPaymentResponse should be $result['HotelPaymentResponse'].

Or don't use the second argument to json_decode().

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, I did try that but still throwing Array ( [0] => )
What does the response from the API look like before you call json_decode()?
I'm not sure hot to check that?
$response = curl_exec($ch); var_dump($response); $result = json_decode($response, true);
So you're getting an empty response from the API. Everything after that is irrelevant, you need to figure out why that's happening.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.