oksuz / curl
A Simple http library uses curl
1.0.2
2015-04-21 18:17 UTC
Requires
- php: >=5.3.0
- ext-curl: *
This package is not auto-updated.
Last update: 2025-02-25 06:02:23 UTC
README
Installation
Composer is recommended for installation.
In one command line :
composer require oksuz/curl dev-master
Or via editting your composer.json
{ "require": { "oksuz/curl": "dev-master" } }
composer update
Examples
Single Requests
GET
$cli = new \Oksuz\Curl\Request("http://example.org"); /** @var \Oksuz\Curl\Response $result */ $result = $cli->result(); //echo $result->getHeader(); //echo $result->getStatusCode(); echo $result->getResponse();
POST
$cli = new \Oksuz\Curl\Request("http://example.org") /** @var \Oksuz\Curl\Response $result */ $result = $cli->post(array("username" => "foo", "password" => "bar")) ->addReferer("http://www.google.com/?q=example") ->setOpt(CURLOPT_USERAGENT, "firefox/2.0.16") // you can add php's CURL_CONSTANTS ->result();
Also available put and delete method
Runing Multiple Curl Requests
$clients = array(); $clients[] = new \Oksuz\Curl\Request("http://example1.org"); $clients[] = new \Oksuz\Curl\Request("http://example2.org"); $clients[] = new \Oksuz\Curl\Request("http://example3.org"); $runner = new \Oksuz\Curl\Runner(); /** @var Array $result contains \Oksuz\Curl\Response */ $result = $runner->runMultiple($cli);
More Complex Example
$curl = new \Oksuz\Curl\Request(); $response = $curl->url("http://www.example.org/login") ->post(array("username" => "user", "password" => "password")) ->addCookieSupport() //OR ->addCookieSupport("/path/to/cookiejar_cookiefile.txt") ->result(); if (200 == $response->getStatusCode()) { $resp = $curl->url("http://www.example.com/private_area") ->get(array("page" => 1, "do" => "show")) // http://www.example.com/private_area?page=1&do=show ->addHeader(array("HTTP_X_REQUESTED_WITH" => "XMLHttpRequest")) ->addCookie(array("cookie_name" => "cookie_value")) ->setBasicAuth("username", "password") ->result(); var_dump($resp); }
Default Values
CURLOPT_FOLLOWLOCATION => true, CURLOPT_USERAGENT => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36", CURLOPT_HEADER => true, CURLOPT_TIMEOUT => 10, CURLOPT_RETURNTRANSFER => true,