php curl集成包的安装和实用

  • Post author:
  • Post category:php


下载curl集成包 https://github.com/php-mod/curl   放在vendor目录下

加载curl包,实例化

vendor(‘Curl.src.Curl.Curl’,”,’.php’);

$pinJ = new \Curl();

然后按照官网demo使用即可

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

$curl = new Curl\Curl();
$curl->get('http://www.example.com/search', array(
    'q' => 'keyword',
));

$curl = new Curl\Curl();
$curl->post('http://www.example.com/login/', array(
    'username' => 'myusername',
    'password' => 'mypassword',
));

$curl = new Curl\Curl();
$curl->setBasicAuthentication('username', 'password');
$curl->setUserAgent('');
$curl->setReferrer('');
$curl->setHeader('X-Requested-With', 'XMLHttpRequest');
$curl->setCookie('key', 'value');
$curl->get('http://www.example.com/');

if ($curl->error) {
    echo $curl->error_code;
}
else {
    echo $curl->response;
}

var_dump($curl->request_headers);
var_dump($curl->response_headers);

$curl = new Curl\Curl();
$curl->setOpt(CURLOPT_RETURNTRANSFER, TRUE);
$curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE);
$curl->get('https://encrypted.example.com/');

$curl = new Curl\Curl();
$curl->put('http://api.example.com/user/', array(
    'first_name' => 'Zach',
    'last_name' => 'Borboa',
));

$curl = new Curl\Curl();
$curl->patch('http://api.example.com/profile/', array(
    'image' => '@path/to/file.jpg',
));

$curl = new Curl\Curl();
$curl->delete('http://api.example.com/user/', array(
    'id' => '1234',
));

$curl->close();

// Example access to curl object.
curl_set_opt($curl->curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1');
curl_close($curl->curl);

// Example of downloading a file or any other content
$curl = new Curl\Curl();
// open the file where the request response should be written
$file_handle = fopen($target_file, 'w+');
// pass it to the curl resource
$curl->setOpt(CURLOPT_FILE, $file_handle);
// do any type of request
$curl->get('https://github.com');
// disable writing to file
$curl->setOpt(CURLOPT_FILE, null);
// close the file for writing
fclose($file_handle);

如果使用composer下载

则使用curl代码

require __DIR__ . ‘/../vendor/autoload.php’;

use \Curl\Curl;

$host = “http://127.0.0.1/?c=user”;

$curl = new Curl();

$uname = ‘apitest_’.rand(10000,99999);

$pwd = ‘apitest123’;

/**

* 注册用户

*/

$curl->post( $host.”&a=register”, array(



‘uname’ => $uname,



‘pwd’


=> $pwd,



));



版权声明:本文为adamlanbot原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。