我正在尝试使用以下选项发送curl请求,但我不知道如何在php curl设置中使用-d选项发送数据。
curl -X 'POST' \
-H 'Content-Type: application/json; charset=utf-8' \
-H 'Authorization: Bearer x'
-v 'URL' \
-d
'{
"input": {
"urn": "num",
"compressedUrn": true,
"rootFilename": "A5.iam"
}
}'
换句话说,我知道如何使用...
发送标头curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer x'
));
但是我不知道-d标志的等效内容。
谢谢
答案 0 :(得分:0)
需要与请求一起发送的数据
我通常将其包装为一个函数,以使错误/成功的处理更加容易。尤其是当您使用Paypal之类的API时
// create the object (you can do this via a string if you want just remove the json encode from the postfields )
$request = new stdClass(); //create a new object
$request->input = new stdClass(); // create input object
$request->input->urn = 'num'; // assign values
$request->input->compressedUrn = true;
$request->input->rootFilename = 'A5.iam';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'URL HERE');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $request ) ); // encode the object to be sent
curl_setopt($ch, CURLOPT_POST, true); // set post to true
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [ //set headers
'Content-Type: application/json',
'Authorization: Bearer x'
]);
$result = curl_exec ($ch);
if ( ! $result) { //check if the cURL was successful.
// do something else if cURL fails
}
curl_close ($ch);
$return = json_decode( $result ); // object if expecting json return
答案 1 :(得分:0)
但是我不知道-d标志的等效内容。
它是CURLOPT_POSTFIELDS。
curl_setopt_array($ch, array(
CURLOPT_URL => 'URL',
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer x',
'Content-Type: application/json; charset=utf-8'
),
CURLOPT_POSTFIELDS => json_encode(array(
'input' => array(
'urn' => 'num',
'compressedUrn' => true,
'rootFilename' => 'A5.iam'
)
))
));