Calling an API from PHP

I have a powershell script that I’m trying to convert to PHP to run on our site. The script is designed to login to a secure site and download a file to the local machine. I want to run it on the site to cut out downloading locally and then uploading to the site. The part that’s giving me problems is:

Invoke-WebRequest -uri “https://www.capnhq.gov/CAP.CapWatchAPI.Web/api/cw?ORGID=$org&unitOnly=$unitOnly” -Headers $headers -OutFile “$filePath” -TimeoutSec 600

The $headers variable includes a login name and encrypted password

I am trying to run the following in my PHP script but seeing no output (a file downloaded to a folder on my site)
[php]
// Get cURL resource
$curl = curl_init();
// Set some options
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => ‘https://www.capnhq.gov/CAP.CapWatchAPI.Web/api/cw?ORGID=$org&unitOnly=$unitOnly’,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
item1 => ‘-Headers $headers’,
item2 => ‘-OutFile $filePath’,
item3 => ‘-TimeoutSec 600’
)
));
curl_exec($curl);
[/php]

You are not capturing the output response from the request.

https://www.sitepoint.com/using-curl-for-remote-requests/

Sponsor our Newsletter | Privacy Policy | Terms of Service