Blogger Information
Blog 19
fans 0
comment 1
visits 19482
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP Curl Post发起网络请求(含请求参数、header、cookie设置)
TANKING的代码日志
Original
1474 people have browsed it

PHP Curl Post示例

  1. <?php
  2. // 返回JSON
  3. header("Content-type:application/json");
  4. // 初始化 CURL
  5. $ch = curl_init();
  6. // 服务器URL
  7. curl_setopt($ch, CURLOPT_URL,'服务器URL');
  8. curl_setopt($ch, CURLOPT_POST, true);
  9. // 设置参数
  10. $data = array(
  11. array(
  12. 'aaa' => 'aaa',
  13. 'bbb' => 'bbb'
  14. )
  15. );
  16. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  17. // 对认证证书来源的检查
  18. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  19. // 从证书中检查SSL加密算法是否存在
  20. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  21. // 获取的信息以文件流的形式返回,而不是直接输出
  22. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  23. // 请求头
  24. $headers[] = "content-type:multipart/form-data;"; // content-type
  25. $headers[] = "user-agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3947.100 Safari/537.36"; // user-agent
  26. $headers[] = "cookie:xxx"; // cookie
  27. curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
  28. // 发起请求并返回请求结果
  29. $result = curl_exec($ch);
  30. // 关闭请求
  31. curl_close($ch);
  32. ?>
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!