Laravel5.4的PHP Curl库-由Ixudra开发

1.composer安装curl插件

composer require ixudra/curl

2.config.php配置

 
 'providers'     => array(
 
        //...
        Ixudra\Curl\CurlServiceProvider::class,
 
    ),
 
 'aliases'       => array(
 
        //...
        'Curl'          => Ixudra\Curl\Facades\Curl::class,
 
    ),

3.Sending GET requests

 use Ixudra\Curl\Facades\Curl;
 
    // 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();

  

4.Sending POST requests

 use Ixudra\Curl\Facades\Curl;
 
    // 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();

5.Sending PUT requests

 use Ixudra\Curl\Facades\Curl;
 
    // Send a PUT request to: http://www.foo.com/bar/1 with arguments 'foz' = 'baz' using JSON
    $response = Curl::to('http://www.foo.com/bar/1')
       ->withData( array( 'foz' => 'baz' ) )
       ->asJson()
       ->put();

  

posted @ 2021-07-06 16:48  ruclouds  阅读(375)  评论(0)    收藏  举报