我有这个卷曲代码我需要将其转换为php
curl -H "Content-Type: application/json" -d '{"command":"sendoffer",
"steamID":"###############","token":"lkTR4VG2", "itemIDsToRequest":["4942877123","4892501549"],
"message": "Message"}' http://website:1337/
正如你所看到的,有一个数组和普通的json。
"itemIDsToRequest":["4942877123","4892501549"]
我查看了许多问题,例如this和this,但无法理解如何实施它。
对不起,对于curl命令我很新。
答案 0 :(得分:2)
该数组是JSON字符串的一部分,该字符串未被解释但在CURL中用作普通字符串数据,因此它并不重要;使用与命令行示例完全相同的JSON字符串,所以:
$data = '{"command":"sendoffer", "steamID":"###############","token":"lkTR4VG2", "itemIDsToRequest":["4942877123","4892501549"], "message": "Message"}'
curl_setopt($ch, CURLOPT_POSTFIELDS, $data );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
答案 1 :(得分:0)
如果你的阵列是
$data = array("command"=>"sendoffer", "steamID"=>"###############","token"=>"lkTR4VG2", "itemIDsToRequest"=>array("4942877123","4892501549"),"message"=>"Message");
然后将其作为json格式发送,使用curl:
$url = 'http://website:1337/';
$ch = curl_init( $url );
// Convert array to json string
$data_json = json_encode( $data );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data_json );
// Indicate in the header that it is json data
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
// Send request
$result = curl_exec($ch);
curl_close($ch);