×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

No response from API
2

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

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

 curl_setopt($ch, CURLOPT_VERBOSE, true);//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

RE: No response from API

Hi

Are you sure that makes a difference ?

CODE --> command line

master # diff craigward-original.php craigward-vacunita.php
28c28
<     curl_setopt($ch, CURLOPT_VERBOSE, true);
---
> //    curl_setopt($ch, CURLOPT_VERBOSE, true);

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

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&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

RE: No response from API

(OP)
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.

RE: No response from API

Quote (feherke)

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

RE: No response from API

(OP)
The actual fix after removing the duplication was to add this to my script.

This fix came from your link http://unitstep.net/blog/2009/05/05/using-curl-in-... 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 = "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_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;	

    ?> 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close