1

I have python api url which is a get request. It gives response in below format:

{"Status":"DONE"}

I am using curl in php to call the api and get the above url but nothing is getting displayed. Below is the code:

<form action="test.php" method="post" enctype="multipart/form-data">

    <input type="submit" name="testapi" value="Test API " />

</form>

<?php 
    if(isset($_POST['testapi']))
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://<api_url>/api/v1/');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $data = curl_exec($ch);
        curl_close($ch);
        echo $data;

    }
?>

Please help. Thanks

Below is the response after adding var_dump(curl_getinfo($ch));

G:\wamp\www\test.php:37:
array (size=26)
  'url' => string 'https://<api_url>/api/v1/' (length=41)
  'content_type' => null
  'http_code' => int 0
  'header_size' => int 0
  'request_size' => int 0
  'filetime' => int -1
  'ssl_verify_result' => int 20
  'redirect_count' => int 0
  'total_time' => float 0.596189
  'namelookup_time' => float 0.047973
  'connect_time' => float 0.338623
  'pretransfer_time' => float 0
  'size_upload' => float 0
  'size_download' => float 0
  'speed_download' => float 0
  'speed_upload' => float 0
  'download_content_length' => float -1
  'upload_content_length' => float -1
  'starttransfer_time' => float 0
  'redirect_time' => float 0
  'redirect_url' => string '' (length=0)
  'primary_ip' => string '52.173.77.140' (length=13)
  'certinfo' => 
    array (size=0)
      empty
  'primary_port' => int 443
  'local_ip' => string '192.168.0.102' (length=13)
  'local_port' => int 52566
S Andrew
  • 5,592
  • 27
  • 115
  • 237
  • Run `var_dump(curl_getinfo($ch));` to see details about how the request went. – Alex Howansky Sep 11 '19 at 16:19
  • Have a look at https://stackoverflow.com/questions/39242332/not-getting-any-error-message-or-content-from-curl and check if this helps in displaying if there are any problems. – Nigel Ren Sep 11 '19 at 16:19
  • @AlexHowansky Where should I run it, after `echo $data;` ? – S Andrew Sep 11 '19 at 16:21
  • Have a look at this code example form the manual https://www.php.net/manual/en/function.curl-getinfo.php#refsect1-function.curl-getinfo-examples – Nigel Ren Sep 11 '19 at 16:24
  • Right after the `curl_exec()` – Alex Howansky Sep 11 '19 at 16:31
  • First try and enter the API URL in your browser to confirm you're getting the expected data back. Next, I'd add an `else echo "nope";` to the bottom of your code, to make sure the `if(isset` isn't the problem. The cURL code you have looks fine. – FoulFoot Sep 11 '19 at 17:33
  • @FoulFoot I have already tested with the things you have mentioned. – S Andrew Sep 12 '19 at 06:10
  • @AlexHowansky I have added the response in the question. – S Andrew Sep 12 '19 at 06:21
  • The `https:` might be the problem. See here: https://stackoverflow.com/questions/4372710/php-curl-https – FoulFoot Sep 12 '19 at 11:31

0 Answers0