CURL 笔记

2016-09-14 16:15:11   php

  php   curl  

POST

  1. $url='';
  2. $data=array('content'=>$xml); //需要请求的数据
  3. $curl=curl_init(); //初始化
  4. curl_setopt($curl,CURLOPT_URL,$url); //POST的URL
  5. curl_setopt($curl,CURLOPT_RETURNTRANSFER,1); //返回数据
  6. curl_setopt($curl,CURLOPT_POST,1);//post方式请求
  7. curl_setopt($curl,CURLOPT_POSTFIELDS,$data); //请求的数据
  8. curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, FALSE); //HTTPS协议是使用
  9. curl_setopt($curl,CURLOPT_SSL_VERIFYHOST, FALSE); //HTTPS协议是使用
  10. $response=curl_exec($curl); //执行请求
  11. curl_close($curl); //关闭
  12. var_dump($response);

GET

  1. $ch = curl_init();
  2. $curl_setopt($ch, CURLOPT_URL, $url);
  3. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// 要求结果为字符串且输出到屏幕上
  4. curl_setopt($ch, CURLOPT_HEADER, 0); // 不要http header 加快效率
  5. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  6. curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, FALSE); //HTTPS协议是使用
  7. curl_setopt($curl,CURLOPT_SSL_VERIFYHOST, FALSE); //HTTPS协议是使用
  8. curl_setopt($ch, CURLOPT_TIMEOUT, 15);
  9. $output = curl_exec($ch);
  10. curl_close($ch);
  11. var_dump($output);