定制 alioygur/curl 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

alioygur/curl

最新稳定版本:v1.0

Composer 安装命令:

composer require alioygur/curl

包简介

A basic CURL wrapper for PHP

README 文档

README

A basic CURL wrapper for PHP (see http://php.net/curl for more information about the libcurl extension for PHP)

Installation

This library is available via Composer

{
    "require": {
        "alioygur/curl": "~1.0"
    }
}

Usage

Simple example of usage

Simply initialize and usage the Curl class like so:

<?php
use Alioygur\Curl\Curl;

$curl = new Curl();

$response = $curl
    ->setOption('CURLOPT_FOLLOW_REDIRECTS', false)
    ->setHeader('User-Agent', 'My Name is Heisenberg!')
    ->get('http://example.com');

Performing a Request

The Curl object supports 5 types of requests: HEAD, GET, POST, PUT, and DELETE. You must specify a url to request and optionally specify an associative array or string of variables to send along with it.

$response = $curl->head($url, $vars = []);
$response = $curl->get($url, $vars = []); # The Curl object will append the array of $vars to the $url as a query string
$response = $curl->post($url, $vars = []);
$response = $curl->put($url, $vars = []);
$response = $curl->delete($url, $vars = []);

To use a custom request methods, you can call the request method:

$response = $curl->request('YOUR_CUSTOM_REQUEST_TYPE', $url, $vars = []);

All of the built in request methods like put and get simply wrap the request method. For example, the post method is implemented like:

function post($url, $vars = []) {
    return $this->request('POST', $url, $vars);
}

Examples:

$response = $curl->get('google.com?q=test');

# The Curl object will append '&some_variable=some_value' to the url
$response = $curl->get('google.com?q=test', array('some_variable' => 'some_value'));

$response = $curl->post('test.com/posts', array('title' => 'Test', 'body' => 'This is a test'));

All requests return a CurlResponse object (see below) or false if an error occurred. You can access the error string with the $curl->error() method.

The CurlResponse Object

A normal CURL request will return the headers and the body in one response string.

# Response Headers -------------------------------------------------------------------------

# Get the response body
echo $response->body(); # A string containing everything in the response except for the headers

# Get the response headers
print_r($response->headers()); # An associative array containing the response headers

# Pick one from response headers
echo $response->headers('Content-Type'); # text/html 

# You can also use those methods 
$response->status(); # 200 OK
$response->statusCode(); # 200
$response->ContentType(); # text/html

# Request Headers --------------------------------------------------------------------------

# Get the request headers
$response->requestHeaders(); # An associative array containing the request headers

# Pick one from request headers 
echo $response->requestHeaders('Version'); # HTTP/1.1

# Curl Information -------------------------------------------------------------------------
Get information regarding a specific transfer. See, http://php.net/manual/en/function.curl-getinfo.php

# Get all
$response->getInfo(); # An associative array containing the curl information

# Pick one
$response->getInfo('total_time'); # 0.14257

The CurlResponse class defines the magic __toString() method which will return the response body, so echo $response is the same as echo $response->body

Cookie Sessions

By default, cookies will be stored in a file called curl_cookie.txt. You can change this file's name by setting it like this

$curl->setCookieFile('some_other_filename');

This allows you to maintain a session across requests

Setting Custom Headers

You can set custom headers to send with the request

$curl->setheader('SOME_KEY', 'some value');

# you can also method chaining
$response = $curl->setHeader('Content-Type', 'application/json')
     ->setHeader('User-Agent', 'Mozilla/5.0 (X11; Linux...')
     ->get('http://example.com');

Setting Custom CURL request options

By default, the Curl object will follow redirects. You can disable this by setting:

$curl->setOptions('CURLOPT_FOLLOW_REDIRECTS', false);

You can set/override many different options for CURL requests (see the curl_setopt() documentation for a list of them)

Auth

Sets the user and password for HTTP auth basic authentication method.

$curl->setAuth('username', 'password');

Contact

Problems, comments, and suggestions all welcome: alioygur@gmail.com

统计信息

  • 总下载量: 51
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-11-05

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固