CURL访问接口

2017-02-28 12:57:17   Laravel

  SOA架构   curl   访问接口  

get访问

  1. public function curl($url){
  2. $curlobj = curl_init();
  3. curl_setopt($curlobj, CURLOPT_URL, $url);
  4. // curl_setopt($curlobj, CURLOPT_HEADER, 0);
  5. curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);
  6. // 验证
  7. curl_setopt($curlobj, CURLOPT_SSL_VERIFYPEER, 0);
  8. curl_setopt($curlobj, CURLOPT_SSL_VERIFYHOST, 0);
  9. $res = curl_exec($curlobj);
  10. $data = json_decode($res);
  11. return $data;
  12. curl_close($curlobj);
  13. }

post访问

  1. public function curlpost($url,$data){
  2. $timeout = 300;
  3. $ch = curl_init();
  4. // http_build_query($params, null, '&')
  5. curl_setopt($ch, CURLOPT_URL, $url); //定义表单提交地址
  6. curl_setopt($ch, CURLOPT_POST, 1); //定义提交类型 1:POST ;0:GET
  7. curl_setopt($ch, CURLOPT_HEADER, 0); //定义是否显示状态头 1:显示 ; 0:不显示
  8. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//定义是否直接输出返回流
  9. curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //定义提交的数据,这里是XML文件
  10. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  11. $res = curl_exec($ch);
  12. curl_close($ch);//关闭
  13. return $res;
  14. }

DELETE访问

  1. public function curld($url){
  2. $timeout = 300;
  3. $curl_handle = curl_init ();
  4. // Set default options.
  5. curl_setopt($curl_handle, CURLOPT_URL, $url); // 要访问的地址
  6. curl_setopt($curl_handle,CURLOPT_HEADER,1); // 是否显示返回的Header区域内容
  7. curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
  8. curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
  9. curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
  10. curl_setopt ( $curl_handle, CURLOPT_CUSTOMREQUEST, 'DELETE' );
  11. $res = curl_exec($curl_handle);
  12. curl_close($curl_handle);//关闭
  13. return $res;
  14. }

put

  1. public function curlput($url,$data){
  2. // $timeout = 300;
  3. // dd($data);
  4. // $data = json_encode($data);
  5. $data = 'data='.$data['link_name'].','.$data['link_title'].','.$data['link_url'].','.$data['link_order'];
  6. // $data=implode('=',$datsa);
  7. // $data['link_name'].$data['link_title'].$data['link_url'].$data['link_url'];
  8. // $data = 'link_name='.$data['link_name'].'link_title='.$data['link_title'].'link_url='.$data['link_url'].'link_order='.$data['link_order'];
  9. $timeout = 300;
  10. $ch = curl_init();
  11. // http_build_query($params, null, '&')
  12. curl_setopt($ch, CURLOPT_URL, $url); //定义表单提交地址
  13. // curl_setopt($ch, CURLOPT_POST, 1); //定义提交类型 1:POST ;0:GET
  14. curl_setopt($ch, CURLOPT_HEADER, 0); //定义是否显示状态头 1:显示 ; 0:不显示
  15. curl_setopt($ch, CURLOPT_HEADER, 0);
  16. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//定义是否直接输出返回流
  17. curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //定义提交的数据,这里是XML文件
  18. ///rl_setopt($ch, CURLOPT_POSTFIELDS, $data); //定义提交的数据,这里是XML文件
  19. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  20. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  21. $res = curl_exec($ch);
  22. curl_close($ch);//关闭
  23. return $res;
  24. }

put 方法 中 data数据比较特殊 需要时 =’‘; 传数组的话 前台接收后数据只有key 没有值

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

感谢你的支持,我会继续努力!
扫码打赏,建议金额1-10元