How to use PHP's cURL command to connect to the FWS API

Once you have your API Keys, you are ready to connect to the FWS API.  When sending data, you can send your request in either JSON or XML.  The response you receive back from the server will be in the same format type that was received.

Below is a function written in PHP that will connect to a given URL and send the request data.

 

Field Type Description
 url string The destination URL
pub_key string Your public key for the FWS API
priv_key string Your private key for the FWS API
data string Either a JSON or XML formatted string containing parameters pertaining to your request.
method string The type of method (POST or GET)
data_type string The data type for the formatted data being sent.

 

function fw_api_call($url = null, $pub_key = null, $priv_key = null, $data = null, $method = 'POST', $data_type = 'application/json') {
    if (!$url) {
        return false;
    }
    
    $ch = curl_init($url);
    
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: '.$data_type,                                                                                
        'Content-Length: ' . strlen($data),
		'public-key: ' . $pub_key,
		'private-key: ' . $priv_key,)                                                                       
    );
    
    $result = curl_exec($ch);
    
    if(curl_errno($ch)) {
        return false;
    }
    
    curl_close($ch);
    
    return $result;
}

As you can see, the public-key and private-key must be sent within the request header, as well as the data-type.  Failure to supply the keys will result in an error.  Failure to supply the data-type will not result in an error, but will default to JSON.