PHP(CURL)POST数据调用API简单示例
PHP(CURL)POST数据调用API简单示例Php代码1.<?php
2./**
3. *@一个完整的POST调用API的过程 百度知道
4. *@author: bo.xiao
5. */
6. $url = 'http://zhidao.chanjet.com/restserver/zhidao';
7. $data = array(
8. 'api_key'=>'3qQ2Edm62Vd4bAVCwNoxgn0l',
9. 'method'=>'baidu.zhidao.getQuestionList',
10. 'call_id'=>'1308713190',
11. 'cid'=>59533,
12. 'qstatus'=>1,
13. 'format'=>'json',
14. 'page_no'=>1,
15. 'page_size'=>25,
16. 'keywords'=>'财务',
17. 'bd_sig'=>'2bad1c47bb75e0363a689f4b09743afb'
18. );
19.
20. $json_data = postData($url, $data);
21. $array = json_decode($json_data,true);
22. echo '<pre>';print_r($array);
23.
24. function postData($url, $data)
25. {
26. $ch = curl_init();
27. $timeout = 300;
28. curl_setopt($ch, CURLOPT_URL, $url);
29. curl_setopt($ch, CURLOPT_POST, true);
30. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
31. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
32. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
33. $handles = curl_exec($ch);
34. curl_close($ch);
35. return $handles;
36. }
37.
38.?>
页:
[1]