如何将CURL命令转换为PHP?

时间:2015-07-29 11:53:46

标签: php curl

有人可以帮我将以下curl命令转换为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" }'

我不确定如何通过PHP设置--key--cert选项以及如何获取响应。

1 个答案:

答案 0 :(得分:0)

作为一个起点,这就是你想要的事情

<?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);

查看PHP here

中cURL的手册页