0

i am looking for an example to call data from API request using CURL in php. Currently i am working on vreasy project, where i need to get data from its API. But in vreasy example, they have shown some like this

curl -u "<your-api-key>:" -X GET "https://api.vreasy.com/reservations?status=ENQUIRY&expand=guest&fields=guest/fname,guest/lname,guest/email"

so, my question how can i use this example to work with my PHP code. I want an example on PHP using this method. I am new to this, sorry for that if this question looks like silly.

I am working on wordpress project, where need to data which retrieve from this API into wordpress custom post type.

i have tried something like this.

<?php
echo 'asd';
$api_key = 'your-api-key';
$url = 'https://api.vreasy.com/reservations?status=ENQUIRY&expand=guest&fields=guest/fname,guest/lname,guest/email';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $api_key . ":"); // Normally you'd put a password after the colon, but you don't need it if you're using an API key
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
$response_json = json_decode($response, true);

print_r($response_json);

curl_close($ch);

but there is no response. Thanks

C Modi
  • 19
  • 8
  • 1
    Take a look cURL in the PHP manual and give it a shot. If you then get stuck on something specific, please come back an show us what you'e tried. SO is not a code conversion service. – M. Eriksson Sep 08 '18 at 09:36
  • @MagnusEriksson i have add code, which i have tried. But i unable to figure out what wrong in that. I have also search on internet and almost all answers shows me this type of code. Could you able to help me? – C Modi Sep 08 '18 at 09:45
  • What does print_r show? – Ivnhal Sep 08 '18 at 10:09
  • blank page, nothing – C Modi Sep 08 '18 at 10:12
  • A blank page could be an error or simply no result. To make sure, checked your servers error log. A good idea is also to turn `display_errors` on in your local PHP environment. Read more here: [How do I get PHP errors to display?](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). – M. Eriksson Sep 08 '18 at 10:21
  • yes, error log is enabled. and there is no error – C Modi Sep 08 '18 at 10:23
  • and of-course, there is data on API server. if you want API key to test, share your email ID or any other private communication method, where i can share sensitive information. – C Modi Sep 08 '18 at 10:41
  • you code working fine. see the fiddle http://phpfiddle.org/main/code/gijm-3prz – Vel Sep 08 '18 at 10:51
  • it returns this response `Array ( [message] => Unauthorized: invalid credentials [exception] => Array ( ) [requestParams] => Array ( [controller] => reservations [action] => index [status] => Array ( [0] => ENQUIRY ) [expand] => guest [fields] => guest/fname,guest/lname,guest/email [module] => api [request-id] => W5Op135MZVl0@-c-lpQDLQAAANE [user_id] => 1 [agent] => user ) [code] => 401 )` – Vel Sep 08 '18 at 10:52
  • ya, right in this it is showing me output, so any idea why this is showing blank page on my localhost? – C Modi Sep 08 '18 at 11:02
  • seems it will not works in localhost – Vel Sep 08 '18 at 11:07
  • just try in any demo server – Vel Sep 08 '18 at 11:08
  • its working on my demo server. please change your host `localhost` to like `http://demo.com/test/` – Vel Sep 08 '18 at 11:13
  • _"so any idea why this is showing blank page on my localhost"_ - Sorry, but it's pretty impossible for us to know why it doesn't work on your local machine. We don't even know what OS/Web server/configuration you're running. It get's even harder if you don't get any error messages – M. Eriksson Sep 08 '18 at 11:18
  • Possible duplicate of [php - login into HTTP basic Auth](https://stackoverflow.com/questions/22765760/php-login-into-http-basic-auth) – hanshenrik Sep 08 '18 at 18:25

1 Answers1

0

Try this code. I have added this lines $error = curl_error($ch); print_r($error);

<?php


    echo 'asd';
    $api_key = 'your-api-key';
    $url = 'https://api.vreasy.com/reservations?status=ENQUIRY&expand=guest&fields=guest/fname,guest/lname,guest/email';

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERPWD, $api_key . ":"); // Normally you'd put a password after the colon, but you don't need it if you're using an API key
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    $response_json = json_decode($response, true);

    print_r($response_json);

    $error = curl_error($ch);

    print_r($error);

    curl_close($ch);

    exit;
?>

you will get this error on localhost. SSL certificate problem

SSL certificate problem: unable to get local issuer certificate

Vel
  • 9,027
  • 6
  • 34
  • 66