PHP Example

Example code to demonstrate calling the Shieldpay API using PHP.

          
            
>?php function guidv4() { $data = random_bytes(16); assert(strlen($data) == 16); $data[6] = chr(ord($data[6]) & 0x0f | 0x40); $data[8] = chr(ord($data[8]) & 0x3f | 0x80); return vsprintf("%s%s-%s-%s-%s-%s%s%s", str_split(bin2hex($data), 4)); } $url = "https://api.sandbox.partner.shieldpay.com/v1/projects"; $apiKey = "my-api-key"; $requestId = guidv4(); // Must be unique for every request. $now = new DateTime("now", new DateTimeZone("UTC")); $timestamp = $now->format("Y-m-d\TH:i:s.") . substr($now->format("u"),0,3) . "Z"; // Must be less than 5 minutes old. // Create the payload for the request. $body = [ "name" => "my example project", // ... add additional properties ]; $bodyPayload = json_encode($body); // Create the string containing the key elements of the request, which we"ll use // to generate the digital signature. For request methods without a body, e.g., GET, // you can omit the body from the string. E.g.: // const signatureString = `${url}${apiKey}${requestId}${timestamp}`; $signatureString = $url . $apiKey . $requestId . $timestamp . $bodyPayload; // Load private key and certificate and generate a digital signature for the request. $privateKey = openssl_pkey_get_private(file_get_contents("/path/to/private.key")); openssl_sign($signatureString, $signature, $privateKey, OPENSSL_ALGO_SHA256); $signature = base64_encode($signature); // Make the API request. If you need to add query params, append them to the URL. E.g., // await fetch(`${url}?page_size=1`, ... $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_POSTFIELDS => $bodyPayload, CURLOPT_RETURNTRANSFER => true, // Configure certificate and private key to establish mTLS connection. CURLOPT_SSLCERT => "/path/to/certificate.pem", CURLOPT_SSLKEY => "/path/to/private.key", // Add headers. CURLOPT_HTTPHEADER => [ "Authorization: $apiKey", "Timestamp: $timestamp", "RequestID: $requestId", "DigitalSignature: $signature", "Content-Type: application/json", ], ]); $response = curl_exec($ch); if(curl_errno($ch)) { echo "Curl error: " . curl_error($ch); die; } $responseJson = json_decode($response, true); print_r($response);