Custom PHP curl library for the Laravel 5 framework - developed by Ixudra
fork from : https://github.com/ixudra/curl.git
|
9 år sedan | |
---|---|---|
public | 11 år sedan | |
src | 9 år sedan | |
tests | 11 år sedan | |
.gitignore | 11 år sedan | |
.travis.yml | 11 år sedan | |
LICENSE | 11 år sedan | |
README.md | 9 år sedan | |
composer.json | 9 år sedan | |
composer.lock | 10 år sedan | |
phpunit.xml | 11 år sedan |
Custom PHP cURL library for the Laravel 4 or 5 framework - developed by Ixudra.
This package can be used by anyone at any given time, but keep in mind that it is optimized for my personal custom workflow. It may not suit your project perfectly and modifications may be in order.
Pull this package in through Composer.
{
"require": {
"ixudra/curl": "6.*"
}
}
Add the service provider to your config/app.php
file:
'providers' => array(
//...
Ixudra\Curl\CurlServiceProvider::class,
),
Add the facade to your config/app.php
file:
'facades' => array(
//...
'Curl' => Ixudra\Curl\Facades\Curl::class,
),
Add the service provider to your app/config/app.php
file:
'providers' => array(
//...
'Ixudra\Curl\CurlServiceProvider',
),
Add the facade to your app/config/app.php
file:
'facades' => array(
//...
'Curl' => 'Ixudra\Curl\Facades\Curl',
),
Create a new instance of the CurlService
where you would like to use the package:
$curlService = new \Ixudra\Curl\CurlService();
The package provides an easy interface for sending cURL requests from your application. The package provides a fluent
interface similar the Laravel query builder to easily configure the request. There are several utility methods that allow
you to easily add certain options to the request. If no utility method applies, you can also use the general withOption
method.
In order to send a GET
request, you need to use the get()
method that is provided by the package:
// Send a GET request to: http://www.foo.com/bar
$response = Curl::to('http://www.foo.com/bar')
->get();
// Send a GET request to: http://www.foo.com/bar?foz=baz
$response = Curl::to('http://www.foo.com/bar')
->withData( array( 'foz' => 'baz' ) )
->get();
// Send a GET request to: http://www.foo.com/bar?foz=baz using JSON
$response = Curl::to('http://www.foo.com/bar')
->withData( array( 'foz' => 'baz' ) )
->asJson()
->get();
Post requests work similar to GET
requests, but use the post()
method instead:
// Send a POST request to: http://www.foo.com/bar
$response = Curl::to('http://www.foo.com/bar')
->post();
// Send a POST request to: http://www.foo.com/bar
$response = Curl::to('http://www.foo.com/bar')
->withData( array( 'foz' => 'baz' ) )
->post();
// Send a POST request to: http://www.foo.com/bar with arguments 'foz' = 'baz' using JSON
$response = Curl::to('http://www.foo.com/bar')
->withData( array( 'foz' => 'baz' ) )
->asJson()
->post();
// Send a POST request to: http://www.foo.com/bar with arguments 'foz' = 'baz' using JSON and return as associative array
$response = Curl::to('http://www.foo.com/bar')
->withData( array( 'foz' => 'baz' ) )
->asJson( true )
->post();
PUT
and DELETE
will be added in the near future.
For downloading a file, you can use the download()
method:
// Download an image from: file http://www.foo.com/bar.png
$response = Curl::to('http://foo.com/bar.png')
->withContentType('image/png')
->download('/path/to/dir/image.png');
You can add various cURL options to the request using of several utility methods such as withHeader()
for adding a
header to the request, or use the general withOption()
method if no utility method applies. The package will
automatically prepend the options with the CURLOPT_
prefix. It is worth noting that the package does not perform
any validation on the cURL options. Additional information about available cURL options can be found
here.
Usage without Laravel is identical to usage described previously. The only difference is that you will not be able to
use the facades to access the CurlService
.
$curlService = new \Ixudra\Curl\CurlService();
// Send a GET request to: http://www.foo.com/bar
$response = $curlService->to('http://www.foo.com/bar')
->get();
// Send a POST request to: http://www.foo.com/bar
$response = $curlService->to('http://www.foo.com/bar')
->post();
PUT
and DELETE
methodThis template is open-sourced software licensed under the MIT license
Jan Oris (developer)