No response from API
No response from API
(OP)
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. https://api.beds24.com/json/getProperty 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 = "https://api.beds24.com/json/getProperty"; $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; ?>
RE: No response from API
If you remove it, it should then work. Though I get a Certificate error.
CODE
----------------------------------
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
RE: No response from API
Are you sure that makes a difference ?
CODE --> command line
BTW, craigward's original code works fine for me on Ubuntu :
CODE --> command line
master # php craigward-original.php 2> /dev/null Took 1.190645 seconds to send a request to https://api.beds24.com/json/getProperty {"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&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
RE: No response from API
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.
RE: No response from API
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
RE: No response from API
This fix came from your link http://unitstep.net/blog/2009/05/05/using-curl-in-... there are two solutions here.
CODE
Here is the code in working order.
CODE