PHP与Curl采用的GET,POST,JSON方式请求API

记录curl用不同方式:GET,POST,JSON等请求一个Api,网上很多例子,我这里也写个笔记,记录一下自己利用不同方式请求api的curl方法。方法可借鉴,可引用
GET方式

/**
 * Function:curl GET 请求
 * @param $url
 * @param array $params
 * @param int $timeout
 * @return mixed
 * @throws Exception
 */
public function request_curl_get($url, $params = array(),$timeout=30){

    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

    $file_contents = curl_exec($ch);
    if($file_contents === false){
        throw new Exception('Http request message :'.curl_error($ch));
    }

    curl_close($ch);
    return $file_contents;

}

POST方式

/**
 * 请求一个地址
 *
 * @param   string $url 需要请求的地址
 * @param   array $post 需要以post的方式发送的数据
 * @param   bool $is_async 是否是异步方式请求;暂未实现
 * @param   int $retry 重试次数;默认0
 * @param   bool $verify_ssl 是否验证 ssl 证书;默认禁用
 * @return  mixed|string
 */
function request_url($url, $post = array(), $is_async = FALSE, $retry = 0, $verify_ssl = false)
{
    if (empty($url)) return '';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);

    // 需要以post的方式发送的数据
    if (!empty($post)) {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($post) ? http_build_query($post): $post);
    }

    // HTTPS
    if (!$verify_ssl) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
    }

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true); // 自动设置Referer
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 返回curl获取到的内容而不是直接输出
    curl_setopt($ch, CURLOPT_HEADER, false); // 不显示返回的header内容
    curl_setopt($ch, CURLOPT_TIMEOUT, 5); // 5秒超时
    $content = curl_exec($ch);

    if($content === false){
        throw new Exception('Http request message :'.curl_error($ch));
    }

    // 重试
    if ($retry > 0 && $content === false) {
        $try = 0;
        do {
            $content = curl_exec($ch); ++$try;
        }
        while ($content === false && $try <= $retry);
    }

    curl_close($ch);
    return $content;
}

JSON方法

/**
 * Function: curl post 用json方式
 * @param $url
 * @param array $postData
 * @return mixed|string
 * @throws Exception
 */
function api_request_curl($url, $postData = array()) {

    if (empty($url)) return '';
    $postData = json_encode($postData);

    $curl = curl_init();  //初始化
    curl_setopt($curl,CURLOPT_URL,$url);  //设置url
    curl_setopt($curl,CURLOPT_HTTPAUTH,CURLAUTH_BASIC);  //设置http验证方法
    curl_setopt($curl, CURLOPT_TIMEOUT,30);
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);  //设置curl_exec获取的信息的返回方式
    curl_setopt($curl,CURLOPT_POST,1);  //设置发送方式为post请求
    curl_setopt($curl,CURLOPT_POSTFIELDS,$postData);  //设置post的数据

    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($postData))
    );

    $result = curl_exec($curl);
    if($result === false){
        throw new Exception('Http request message :'.curl_error($curl));
    }

    curl_close($curl);
    return $result;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`Apipost` 是一款强大的 API 测试工具,它允许开发者模拟 HTTP 请求来测试 Web 服务。如果你想使用 `Apipost` 来运行 `curl` 命令,通常你不需要直接在 Apipost 中执行 `curl`,因为 Apipost 提供了图形化界面来发送和管理 API 请求。不过,如果你确实想在 Apipost 中模拟一个 `curl` 请求,你可以按照以下步骤操作: 1. **打开 Apipost**:打开 Apipost 应用程序,创建一个新的 API 测试场景。 2. **创建请求**:点击 "New Request" 或者 "Create" 按钮,选择 "HTTP" 类型,并输入你要测试的 API 的 URL。 3. **配置请求方法和数据**:在 "Method" 下拉菜单中选择 `GET`, `POST`, `PUT`, `DELETE` 等,就像在命令行的 `curl` 中一样。在 "Body" 或 "Payload" 标签下,如果需要发送 JSON 或其他数据,可以编辑相应字段。 4. **模拟 curl 命令**:为了使请求看起来像 `curl`,在 "Headers" 部分,你可以添加与 `curl` 中 `-H` 参数相同的自定义头信息。比如,如果你想添加一个 "Authorization" 头,可以在 "Custom Headers" 中输入 `"Authorization: Bearer your-token"`。 5. **保存并运行**:设置好所有参数后,点击 "Save" 保存这个请求,然后点击 "Run" 或 "Send" 按钮,Apipost 将会执行这个模拟的 `curl` 请求并显示响应。 6. **查看结果**:你可以看到请求的详细信息以及返回的响应数据。如果一切正常,你就可以通过 Apipost 的结果来验证 API 的行为是否符合预期。 如果你想在 Apipost 中直接执行 `curl` 命令,可能需要使用 Apipost 的脚本功能,但这通常不是首选的方法,因为它可能不支持所有的 `curl` 特性,并且可能会有额外的学习曲线。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值