PHP(CURL)POST/GET数据调用API示例 1. [代码] [PHP]代码 ---------------------post---------------------------/* * *获取最新视频/全部视频列表getNewList作用:获取用户最新上传视频的列表URLhttp://v.polyv.ne
1. [代码][PHP]代码
---------------------post--------------------------- /* * * 获取最新视频/全部视频列表 getNewList 作用:获取用户最新上传视频的列表 URL http://v.polyv.net/uc/services/rest?method=getNewList 支持格式 XML/JSON HTTP请求方式 POST,GET */ $url = 'http://v.polyv.net/uc/services/rest'; $data = array( 'readtoken'=>'', 'method'=>'getNewList', 'catatree'=>'1,1464143968874', 'numPerPage'=>2, 'pageNum'=>1 ); $json_data = postData($url, $data); //$array = json_decode($json_data,true); //$array = json_decode($json_data); echo "<pre>"; print_r($json_data); echo "</pre>"; /** * @param $url * @param $data * @return mixed */ function postData($url, $data) { $ch = curl_init(); $timeout = 300; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $handles = curl_exec($ch); curl_close($ch); return $handles; } ------------------get--------------------------- //初始化 $ch = curl_init(); //设置选项,包括URL curl_setopt($ch, CURLOPT_URL, "http://v.polyv.net/uc/services/rest?method=getPlayListById&readtoken=p66RaX4rJN-mjIOWB2jQ5-lXVB4uTZUP&id=1464181164323"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); //执行并获取HTML文档内容 $output = curl_exec($ch); //释放curl句柄 curl_close($ch); //打印获得的数据 print_r($output);