Calling an API from PHP

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
agrv8tor
New php-forum User
New php-forum User
Posts: 1
Joined: Tue Jan 23, 2018 4:20 pm

Tue Jan 23, 2018 4:24 pm

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. ... =$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)

// 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. ... =$unitOnly',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
item1 => '-Headers $headers',
item2 => '-OutFile $filePath',
item3 => '-TimeoutSec 600'
)
));
curl_exec($curl);

I should not have to capture the output response, the API being called should save the file (a zip file) to the $filepath which is the web folder that the script resides in.

chorn
php-forum Fan User
php-forum Fan User
Posts: 559
Joined: Fri Apr 01, 2016 2:18 am

Mon Jan 29, 2018 11:29 pm

agrv8tor wrote:
Tue Jan 23, 2018 4:24 pm
I should not have to capture the output response, the API being called should save the file (a zip file) to the $filepath which is the web folder that the script resides in.
And how did you manage to grant filesystem access on your local machine to the API-server? According to the manual, your shell-script is actually saving the contents
-OutFile
Specifies the output file for which this cmdlet saves the response body.
so i do not see why your PHP-script shouldn't do this.

Post Reply