首页 PHP 库 其它类库 php-curl-class-masterPHP的Curl类
php-curl-class-masterPHP的Curl类
<?php
//curl类
class Curl
{
 function Curl(){
  return true;
 }
 function execute($method, $url, $fields='', $userAgent='', $httpHeaders='', $username='', $password=''){
  $ch = Curl::create();
  if(false === $ch){
   return false;
  }
  if(is_string($url) && strlen($url)){
   $ret = curl_setopt($ch, CURLOPT_URL, $url);
  }else{
   return false;
  }
  //是否显示头部信息
  curl_setopt($ch, CURLOPT_HEADER, false);
  //
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  if($username != ''){
   curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
  }
  $method = strtolower($method);
  if('post' == $method){
   curl_setopt($ch, CURLOPT_POST, true);
   if(is_array($fields)){
    $sets = array();
    foreach ($fields AS $key => $val){
     $sets[] = $key . '=' . urlencode($val);
    }
    $fields = implode('&',$sets);
   }
   curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
  }else if('put' == $method){
   curl_setopt($ch, CURLOPT_PUT, true);
  }

GET用法:

$curl = new Curl();
$curl->get('http://www.XXX.com/');

POST用法: 

$curl = new Curl();
$curl->get('http://www.XXX.com/', 'p=1&time=0');


免责声明

本站所有资源均由网友贡献或各大下载网站转载。请自行检查软件的完整性!本站所有资源仅供学习参考。请不要将它们用于商业目的。否则,一切后果由您负责!如有侵权,请联系我们删除。联系方式:admin@php.cn

相关文章

PHP 的 `::class` 如何简化类名处理? PHP 的 `::class` 如何简化类名处理?

26 Nov 2024

在 PHP 的 ::class 表示法中使用 ::class 确定类名是一种有用的机制,它返回类的完全限定名称,包括...

如何调试 PHP cURL 请求中的 POST 字段? 如何调试 PHP cURL 请求中的 POST 字段?

31 Dec 2024

调试 PHP cURL 请求中的 Post 字段了解现有的 cURL 库可能具有挑战性,尤其是在调试请求时。到...

如何用PHP的cURL库发送包含JSON数据的POST请求? 如何用PHP的cURL库发送包含JSON数据的POST请求?

01 Apr 2025

使用PHP的cURL库发送JSON数据在PHP开发中,经常需要与外部API进行交互,其中一种常见的方式是使用cURL库发送POST�...

PHP cURL库如何发送包含JSON数据的POST请求? PHP cURL库如何发送包含JSON数据的POST请求?

01 Apr 2025

利用PHP的cURL库发送JSON格式的POST请求在PHP开发中,经常需要与外部接口进行交互,其中POST请求是常见的一种方�...

cURL 可以优化 PHP 中的 HTTP 持久连接以增强性能吗? cURL 可以优化 PHP 中的 HTTP 持久连接以增强性能吗?

24 Oct 2024

本文讨论使用 PHP 中的 cURL 库优化 HTTP 持久连接。它强调了维护持久连接以增强性能和减少网络中断的重要性,特别是对于繁重的 HTTP 请求

如何将复杂的 cURL 命令转换为 PHP cURL? 如何将复杂的 cURL 命令转换为 PHP cURL?

18 Dec 2024

将命令行 cURL 转换为 PHP cURL 当面对像所提供的那样复杂的 cURL 命令时,将其转换为 PHP cURL 可能是一项令人畏惧的...

See all articles