- 论坛徽章:
- 0
|
php中使用curl发送JSON数据
在PHP中,可以使用curl去发送JSON数据,例子如下:
Java代码- 1.$data = array("name" => "Hagrid", "age" => "36");
- 2.$data_string = json_encode($data);
- 3.
- 4.$ch = curl_init('http://api.local/rest/users');
- 5.curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
- 6.curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
- 7.curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- 8.curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 9. 'Content-Type: application/json',
- 10. 'Content-Length: ' . strlen($data_string))
- 11.);
- 12.
- 13.$result = curl_exec($ch);
复制代码 |
|