-1

I'm trying to send a get request using PHP and curl to retrieve a JSON array using rest API request works fine when I test it by entering the address in chrome address bar or even in command prompt or PowerShell , but it returns an empty array using PHP curl , here is my code

<?php
$baseUrl="https://amitex.biz/delegateapi/createhotvoucher";
    
$myUrl=$baseUrl.'?'.http_build_query(array(
'SecurityCod'=>'19648A15',
'password'=>'xyziuqjq&',
'HotVoucherAmount'=>'1'
)); 

echo $myUrl;

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL,$myUrl);
$content = curl_exec($ch);
echo $content . "<br>";
print_r($_GET);

result > enter image description here

when i enter the address in browser it returns somting like this > enter image description here

I have tried below solutions like how to act like a browser to send get request and how to retrieve JSON using curl , but still not working

How can I emulate a get request exactly like a web browser?

GET JSON data using CURL

Neo Mn
  • 36
  • 11
  • 1
    Remove quotes from `curl_setopt($ch, CURLOPT_URL,'$myUrl');` and write as `curl_setopt($ch, CURLOPT_URL,$myUrl);` – nice_dev Sep 29 '21 at 13:52
  • 1
    That's ok , i was testing with hard coded url , just forgot to remove them – Neo Mn Sep 29 '21 at 14:14
  • don't use echo for debugging, use var_dump. there's a good chance curl is actually returning bool(false), but echo is incapable of displaying bool(false), that's where var_dump comes in. also check curl_errno($ch) and curl_error($ch), what do they say? – hanshenrik Sep 29 '21 at 14:19
  • Add curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); and curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); However, you should add proper certs when making it live. – nice_dev Sep 29 '21 at 14:48
  • @hanshenrik i have tried your two recommendation before but it returns no error and curl_exec just return an empty array – Neo Mn Sep 29 '21 at 15:02
  • 1
    @NeoMn curl_exec NEVER returns an array, it can only return 2 things: `bool` and `string`. what did it return, exactly? – hanshenrik Sep 30 '21 at 06:29
  • @hanshenrik i had a play with my code and now get this error from curl_error() method >> Maximum (20) redirects followed – Neo Mn Oct 01 '21 at 15:35
  • well , finally resolved by setting some cookie file [Here is how to do that ](https://stackoverflow.com/questions/41888514/curl-error-maximum-20-redirects-followed) – Neo Mn Oct 01 '21 at 17:15

1 Answers1

0

I know sometimes curl has issues with https. There are some useful posts here that cover that topic:

PHP CURL & HTTPS

How to send HTTPS posts using php

How to make an HTTPS request using cURL?

Rex Banner
  • 71
  • 6
  • I'll test that for sure , thanks – Neo Mn Sep 29 '21 at 14:19
  • is your curl script running on the same server you are trying to call to from your curl script? – Rex Banner Sep 29 '21 at 15:59
  • oh and I am pretty sure where you call, print_r($_GET); that should be on the script of the url you are trying access (https://amitex.biz/delegateapi/createhotvoucher), not on the page that runs the curl – Rex Banner Sep 29 '21 at 16:03
  • I'm trying to send a get request using curl from my server to another website . so , No – Neo Mn Oct 01 '21 at 15:46