教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 关于PHP的curl功能扩展基本用法

关于PHP的curl功能扩展基本用法

发布时间:2020-09-30   编辑:jiaochengji.com
教程集为您提供关于PHP的curl功能扩展基本用法等资源,欢迎您收藏本站,我们将为您提供最新的关于PHP的curl功能扩展基本用法资源
PHP的curl功能提供了很多函数,需要将这些函数按特定的步骤组合到一起,我们先来了解下PHP建立curl请求的基本步骤。

$ch = curl_init(); // 创建一个新的CURL资源赋给变量$ch curl_setopt($ch, CURLOPT_URL, $url); // 设置URL $response = curl_exec($ch); // 执行,获取URL并输出到浏览器 curl_close($ch); // 释放资源

如果我们希望获取内容但不输出,可以使用 CURLOPT_RETURNTRANSFER 参数,并设置其值为非0或者true值。

代码如下:

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

我们可以通设置函数curl_setopt()的不同参数,可以获得不同的结果,这也是CURL扩展的强大之处。curl_setopt()函数的常用参数选项具体可查阅官方文档,此处就不列举。

下面是我常用的curl get和post请求的方法:

get请求:

public function httpGet(string $url = '')     {         // 记录请求信息的日志         // todo                  try {             $ch = curl_init();             curl_setopt($ch, CURLOPT_URL, $url);             curl_setopt($ch, CURLOPT_HEADER, 0);             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);             curl_setopt($ch, CURLOPT_TIMEOUT, 60);             //https 请求             if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);                 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);             }             $response = curl_exec($ch);             $errorCode = curl_errno($ch);             curl_close($ch);             if (!empty($errorCode)) {                 // 可记录错误码日志                 return null;             }             // 记录返回结果日志             return $response;         } catch (\Exception $e) {             $errorLog = [                 'msg' => $e->getMessage(),                 'trace' => $e->getTraceAsString(),                 'data' => [                     'url' => $url,                 ]             ];             // 记录错误日志             return null;         }     } ```php

POST请求:

public function httpPost(string $url = '', array $data = []) {         // 记录请求信息的日志         // todo     try {         $jsonData = json_encode($data);         $curl = curl_init();         curl_setopt($curl, CURLOPT_URL, $url);         curl_setopt($curl, CURLOPT_POST, 1);         curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonData);         curl_setopt($curl, CURLOPT_HEADER, 0);         curl_setopt($curl, CURLOPT_TIMEOUT, 60);         curl_setopt($curl, CURLOPT_HTTPHEADER, [             'Content-Type: application/json; charset=utf-8',             'Content-Length:' . strlen($jsonData)         ]);         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);         //https 请求         if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {             curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);             curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);         }         $result = curl_exec($curl);         $errorCode = curl_errno($curl);         curl_close($curl);         if (!empty($errorCode)) {             // 可记录错误码日志             return null;         }         // 记录返回结果日志         return json_decode($result, true);     } catch (\Exception $e) {         $errorData = [             'msg' => $e->getMessage(),             'trace' => $e->getTraceAsString(),             'data' => [                 'url' => $url,                 'postData' => $data             ]         ];         // 记录错误日志         return null;     } }

本文系转载,原文地址是:

https://tsmliyun.github.io/php/PHP的CURL功能扩展基本用法/

推荐:PHP视频教程

以上就是关于PHP的curl功能扩展基本用法的详细内容,更多请关注教程集其它相关文章!

  • 本文转载于:tsmliyun,如有侵犯,请联系jquerycn@qq.com删除
  • 您可能感兴趣的文章:
    php下载css中图片函数
    关于PHP的curl功能扩展基本用法
    curl模拟http请求范例详析
    curl是php自带的吗
    PHP添加CURL扩展库的二种方法
    php检测页面是否被百度收录的实例代码
    php如何使用curl?(用法介绍)
    php中开启curl扩展的方法详解
    php中的curl使用心得详解
    php curl模拟post请求的例子

    [关闭]
    ~ ~