Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

No response from API 2

Status
Not open for further replies.

craigward

Programmer
Nov 13, 2007
230
GB
Hi, I am new to PHP and am trying to connect to a JSON API where I will retrieve some data from a property booking system. The API provider provides sample code see below, you just add your API Key and Property ID, when the code is loaded you should see the property details in JSON format. I have setup PHP with cURL on a windows server, PHP runs fine but this code will not retrieve any data. Here is the link to the API and sample code. It was a simple copy and paste, I am not getting any errors so I am struggling to debug this. Please could someone look and let me know what is wrong? The API key is to a live test system so you can run this your end. Thanks for looking.

Code:
    <?php

    /*
    * The following sample uses PHP arrays to construct the JSON data and php-curl to post it to the API.
    * This sample will get the property information. 
    * Change the apiKey and propKey to values for your account to use and test.
    */

    $authentication = array();
    $authentication['apiKey'] = '123456789101112131415167';
    $authentication['propKey'] = '99999999999999999';

    $data = array();    
    $data['authentication'] = $authentication;
    $json = json_encode($data);

    $url = "[URL unfurl="true"]https://api.beds24.com/json/getProperty";[/URL]

    $ch=curl_init();
    curl_setopt($ch, CURLOPT_POST, 1) ;
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    $result = curl_exec($ch);

    curl_setopt($ch, CURLOPT_VERBOSE, true);

    if (!curl_errno($ch)) { // $ch (curl resource) instead of $url (string)
    $info = curl_getinfo($ch);
      echo 'Took ', $info['total_time'], ' seconds to send a request to ', $info['url'], "\n";
    }


    curl_close ($ch);
    echo $result;	

    ?>
 
You are issuing Verbose statement twice. The second one after you execute the curl request, and effectively wipe out what its been done.

If you remove it, it should then work. Though I get a Certificate error.
Code:
 curl_setopt($ch, CURLOPT_VERBOSE, true);
    $result = curl_exec($ch);

 [b]curl_setopt($ch, CURLOPT_VERBOSE, true);[/b]//This one should not be here.



----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
Hi

Are you sure that makes a difference ?
Code:
[blue]master #[/blue] diff craigward-original.php craigward-vacunita.php
28c28
<     curl_setopt($ch, CURLOPT_VERBOSE, true);
---
> //    curl_setopt($ch, CURLOPT_VERBOSE, true);

[blue]master #[/blue] diff <( php craigward-original.php 2>&1 ) <( php craigward-vacunita.php 2>&1 )
33c33
< Took 1.19353 seconds to send a request to [URL unfurl="true"]https://api.beds24.com/json/getProperty[/URL]
---
> Took 1.1926 seconds to send a request to [URL unfurl="true"]https://api.beds24.com/json/getProperty[/URL]

BTW, craigward's original code works fine for me on Ubuntu :
Code:
[blue]master #[/blue] php craigward-original.php 2> /dev/null
Took 1.190645 seconds to send a request to [URL unfurl="true"]https://api.beds24.com/json/getProperty[/URL]
{"getProperty":[{"name":"Property 1","propId":"58752","propTypeId":"16","ownerId":"33440","currency":"GBP","address":"","city":"","state":"","latitude":"0.0000000","longitude":"0.0000000","phone":"","mobile":"","fax":"","email":"sergio@meetingstone.com","web":"","cutOffHour":"24","template1":"","template2":"","template3":"","template4":"","template5":"","template6":"","template7":"","template8":"","notifyUrl":"","agodaComPropertyCode":"","bookingComPropertyCode":"","expediaComUsername":"","expediaComPassword":"","expediaComPropertyCode":"","expediaComCurrency":"USD","icalExportTokenSalt":"","icalExportDescription":"","icalImportOption":"0","roomTypes":[{"name":"Room 1","qty":"1","roomId":"136298","roomSize":"0","maxPeople":"2","maxAdult":"0","maxChildren":"0","minPrice":"40.00","rackRate":"50.00","cleaningFee":"0.00","securityDeposit":"0.00","taxPercent":"0.00","taxPerson":"0.00","unitAllocationPerGuest":"0","unitNames":"","highlightColor":"ffffff","excludeReports":"0","template1":"","template2":"","template3":"","template4":"","template5":"","template6":"","template7":"","template8":"","dependentRoomId1":"0","dependentRoomId2":"0","dependentRoomId3":"0","dependentRoomId4":"0","dependentRoomId5":"0","dependentRoomId6":"0","dependentRoomId7":"0","dependentRoomId8":"0","dependentRoomId9":"0","dependentRoomId10":"0","dependentRoomId11":"0","dependentRoomId12":"0","dependentRoomLogic":"2","dailyPriceCount":"1","agodaComEnableInventory":0,"agodaComEnablePrice":0,"agodaComEnableBooking":0,"agodaComRoomCode":"","airbnbComEnableInventory":0,"airbnbComEnableBooking":0,"airbnbComRoomCode":"","bookingComEnableInventory":0,"bookingComEnableBooking":0,"bookingComRoomCode":"","bookingComRateCode":"","expediaComEnableInventory":0,"expediaComEnablePrice":0,"expediaComEnableBooking":0,"expediaComRoomCode":"","expediaComRateCode":"","icalExportEnableType":"0","icalExportUrl":"https:\/\/api.beds24.com\/ical\/bookings.ics?roomid=136298&amp;token=cd2bc326bed2fc6c9336afd22065220b","icalImport1EnableType":"0","icalImport1Url":"","icalImport2EnableType":"0","icalImport2Url":"","icalImport3EnableType":"0","icalImport3Url":""}]}]}

On Windows you may have a certificate problem as described on curl - SSL CA Certificates. ( Not had to go through it myself so no idea about its practical value, but Using cURL in PHP to access HTTPS (SSL/TLS) protected sites looks more descriptive. )


Feherke.
feherke.github.io
 
Thank you both for looking into this, there was a duplication of that code which I have now removed. Tested again and still no results in my two windows environments.

Feherke, Thank you for testing it in Ubunto that at least verifies that the code is working and that there must be a problem as you suggested, I will investigate this further now.

Could I ask another question? The aim for me is to send and receive data between the API server and my Windows server. I need to sync the data for properties and bookings between two systems. On my side I am using Windows Server and MS SQL. Would you say that PHP would be a good solution to achieve this, or from your experience would you recommend looking at alternative options. You may not have the answer to this but thought I would ask. Many thanks.
 
feherke said:
Are you sure that makes a difference ?

It did for me. Removing the second VERBOSE call let me see the actual error, which was the SSL error. Probably because I tested on Windows yes.

Haven't tried on Linux yet.







----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
The actual fix after removing the duplication was to add this to my script.

This fix came from your link there are two solutions here.


Code:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

Here is the code in working order.

Code:
<?php

    /*
    * The following sample uses PHP arrays to construct the JSON data and php-curl to post it to the API.
    * This sample will get the property information. 
    * Change the apiKey and propKey to values for your account to use and test.
    */

    $authentication = array();
    $authentication['apiKey'] = '123456789101112131415167';
    $authentication['propKey'] = '99999999999999999';

    $data = array();    
    $data['authentication'] = $authentication;
    $json = json_encode($data);

    $url = "[URL unfurl="true"]https://api.beds24.com/json/getProperty";[/URL]

    $ch=curl_init();
    curl_setopt($ch, CURLOPT_POST, 1) ;
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    $result = curl_exec($ch);


    if (!curl_errno($ch)) { // $ch (curl resource) instead of $url (string)
    $info = curl_getinfo($ch);
      echo 'Took ', $info['total_time'], ' seconds to send a request to ', $info['url'], "\n";
    }


    curl_close ($ch);
    echo $result;	

    ?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top