0

Could someone please help me to convert the following curl command to PHP:

curl --key ~/Desktop/private.pem --cert ~/Desktop/cert.crt --header "Content-type: application/json; profile=http://example.com/docs/v1.0/schemas/list.json" https://example.com/sandbox/v1/list --data '{ "foo" : "bar" }'

I'm not sure how to set the --key and --cert options via PHP and also how to get the response.

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
Matthew Davis
  • 398
  • 1
  • 7

1 Answers1

0

As a starting point, this is the sort of thing you're after

<?php
$ch=curl_init($url);

curl_setopt($ch, CURLOPT_URL, "https://example.com/sandbox/v1/list");

// Capture the output
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

//Set the key options
curl_setopt($ch, CURLOPT_SSLKEY, "~/Desktop/private.pem"); // Need to change to an absolute path
curl_setopt($ch, CURLOPT_SSLKEYPASSWD, "password");

// Set the certificate options
curl_setopt($ch, CURLOPT_SSLCERT, "~/Desktop/cert.crt"); // Need to change to absolute path
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, "password");

// Set the headers
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/json; profile=http://example.com/docs/v1.0/schemas/list.json"));

//Set the data
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{ "foo" : "bar" }');

// Run the request
$result=curl_exec($ch);

curl_close($ch);

Check out the manual page for cURL in PHP here

DaveyBoy
  • 2,928
  • 2
  • 17
  • 27
  • You may have to mess around with the `CURLOPT_SSLKEY` and `CURLOPT_SSLCERT` options - not sure if I got them round the right way. Either way, they need to be absolute values as your PHP may not have the same environment as you (different `$HOME` etc) – DaveyBoy Jul 29 '15 at 12:54