0

Need to access REST api from php using cURL. This i a rest link:

http://username:password@172.16.59.225:8042/tools/find -d '{"Level":"Study","Expand":false,"Query":{"StudyDate":"20191226-20200324"}}'

as you see a lot of " and white space.

I have tried to prepare string manually like this:

$url = "http://172.16.59.225/tools/find -d '{\"Level\":\"Study\",\"Expand\":false,\"Query\":{\"StudyDate\":\"20191226-20200324\"}}'";

and pass password using: curl_setopt($ch, CURLOPT_USERPWD, "username:password");

I have used urlencode() and curl_escape and nothing works. At the same time, this link works perfectly by using it with curl in Linux cmd.

Is it doable?

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Kriss
  • 435
  • 1
  • 10
  • 22

1 Answers1

1

Try this, --data or -d param is to sends the specified data in a POST request to the HTTP server Source

$url = 'http://172.16.59.225/tools/find';
$postdata = '{"Level":"Study","Expand":false,"Query":{"StudyDate":"20191226-20200324"}}';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
print_r ($result);
Illya
  • 1,268
  • 1
  • 5
  • 16