2015-08-17 17:00:26 -07:00
< ? php
/*
* This file is part of the Goutte package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Goutte ;
use GuzzleHttp\Client as GuzzleClient ;
use GuzzleHttp\ClientInterface as GuzzleClientInterface ;
2015-08-27 12:03:05 -07:00
use GuzzleHttp\Cookie\CookieJar ;
2015-08-17 17:00:26 -07:00
use GuzzleHttp\Exception\RequestException ;
2015-08-27 12:03:05 -07:00
use Psr\Http\Message\ResponseInterface ;
2015-08-17 17:00:26 -07:00
use Symfony\Component\BrowserKit\Client as BaseClient ;
2015-10-08 11:40:12 -07:00
use Symfony\Component\BrowserKit\Request ;
2015-08-17 17:00:26 -07:00
use Symfony\Component\BrowserKit\Response ;
/**
* Client .
*
* @ author Fabien Potencier < fabien . potencier @ symfony - project . com >
* @ author Michael Dowling < michael @ guzzlephp . org >
2015-08-27 12:03:05 -07:00
* @ author Charles Sarrazin < charles @ sarraz . in >
2015-08-17 17:00:26 -07:00
*/
class Client extends BaseClient
{
protected $client ;
private $headers = array ();
private $auth = null ;
public function setClient ( GuzzleClientInterface $client )
{
$this -> client = $client ;
return $this ;
}
public function getClient ()
{
if ( ! $this -> client ) {
$this -> client = new GuzzleClient ( array ( 'defaults' => array ( 'allow_redirects' => false , 'cookies' => true )));
}
return $this -> client ;
}
public function setHeader ( $name , $value )
{
$this -> headers [ $name ] = $value ;
return $this ;
}
public function removeHeader ( $name )
{
unset ( $this -> headers [ $name ]);
}
public function setAuth ( $user , $password = '' , $type = 'basic' )
{
$this -> auth = array ( $user , $password , $type );
return $this ;
}
public function resetAuth ()
{
$this -> auth = null ;
return $this ;
}
2015-10-08 11:40:12 -07:00
/**
* @ param Request $request
*
* @ return Response
*/
2015-08-17 17:00:26 -07:00
protected function doRequest ( $request )
{
$headers = array ();
foreach ( $request -> getServer () as $key => $val ) {
$key = strtolower ( str_replace ( '_' , '-' , $key ));
$contentHeaders = array ( 'content-length' => true , 'content-md5' => true , 'content-type' => true );
if ( 0 === strpos ( $key , 'http-' )) {
$headers [ substr ( $key , 5 )] = $val ;
}
// CONTENT_* are not prefixed with HTTP_
elseif ( isset ( $contentHeaders [ $key ])) {
$headers [ $key ] = $val ;
}
}
2015-08-27 12:03:05 -07:00
$cookies = CookieJar :: fromArray (
$this -> getCookieJar () -> allRawValues ( $request -> getUri ()),
2015-10-08 11:40:12 -07:00
parse_url ( $request -> getUri (), PHP_URL_HOST )
2015-08-27 12:03:05 -07:00
);
2015-08-17 17:00:26 -07:00
$requestOptions = array (
2015-08-27 12:03:05 -07:00
'cookies' => $cookies ,
2015-08-17 17:00:26 -07:00
'allow_redirects' => false ,
2015-08-27 12:03:05 -07:00
'auth' => $this -> auth ,
2015-08-17 17:00:26 -07:00
);
2015-08-27 12:03:05 -07:00
if ( ! in_array ( $request -> getMethod (), array ( 'GET' , 'HEAD' ))) {
if ( null !== $content = $request -> getContent ()) {
$requestOptions [ 'body' ] = $content ;
} else {
if ( $files = $request -> getFiles ()) {
$requestOptions [ 'multipart' ] = [];
$this -> addPostFields ( $request -> getParameters (), $requestOptions [ 'multipart' ]);
$this -> addPostFiles ( $files , $requestOptions [ 'multipart' ]);
} else {
$requestOptions [ 'form_params' ] = $request -> getParameters ();
}
}
}
2015-08-17 17:00:26 -07:00
if ( ! empty ( $headers )) {
$requestOptions [ 'headers' ] = $headers ;
}
2015-08-27 12:03:05 -07:00
$method = $request -> getMethod ();
$uri = $request -> getUri ();
2015-08-17 17:00:26 -07:00
foreach ( $this -> headers as $name => $value ) {
2015-08-27 12:03:05 -07:00
$requestOptions [ 'headers' ][ $name ] = $value ;
2015-08-17 17:00:26 -07:00
}
// Let BrowserKit handle redirects
try {
2015-08-27 12:03:05 -07:00
$response = $this -> getClient () -> request ( $method , $uri , $requestOptions );
2015-08-17 17:00:26 -07:00
} catch ( RequestException $e ) {
$response = $e -> getResponse ();
if ( null === $response ) {
throw $e ;
}
}
return $this -> createResponse ( $response );
}
2015-08-27 12:03:05 -07:00
protected function addPostFiles ( array $files , array & $multipart , $arrayName = '' )
2015-08-17 17:00:26 -07:00
{
2015-08-27 12:03:05 -07:00
if ( empty ( $files )) {
return ;
}
2015-08-17 17:00:26 -07:00
foreach ( $files as $name => $info ) {
if ( ! empty ( $arrayName )) {
$name = $arrayName . '[' . $name . ']' ;
}
2015-08-27 12:03:05 -07:00
$file = [
'name' => $name ,
];
2015-08-17 17:00:26 -07:00
if ( is_array ( $info )) {
if ( isset ( $info [ 'tmp_name' ])) {
if ( '' !== $info [ 'tmp_name' ]) {
2015-08-27 12:03:05 -07:00
$file [ 'contents' ] = fopen ( $info [ 'tmp_name' ], 'r' );
if ( isset ( $info [ 'name' ])) {
$file [ 'filename' ] = $info [ 'name' ];
}
2015-08-17 17:00:26 -07:00
} else {
continue ;
}
} else {
2015-08-27 12:03:05 -07:00
$this -> addPostFiles ( $info , $multipart , $name );
continue ;
2015-08-17 17:00:26 -07:00
}
} else {
2015-08-27 12:03:05 -07:00
$file [ 'contents' ] = fopen ( $info , 'r' );
2015-08-17 17:00:26 -07:00
}
2015-08-27 12:03:05 -07:00
$multipart [] = $file ;
2015-08-17 17:00:26 -07:00
}
}
2015-08-27 12:03:05 -07:00
public function addPostFields ( array $formParams , array & $multipart , $arrayName = '' )
2015-08-17 17:00:26 -07:00
{
2015-08-27 12:03:05 -07:00
foreach ( $formParams as $name => $value ) {
if ( ! empty ( $arrayName )) {
$name = $arrayName . '[' . $name . ']' ;
}
2015-08-17 17:00:26 -07:00
2015-08-27 12:03:05 -07:00
if ( is_array ( $value )) {
$this -> addPostFields ( $value , $multipart , $name );
} else {
$multipart [] = [
'name' => $name ,
'contents' => $value ,
];
}
}
}
protected function createResponse ( ResponseInterface $response )
{
return new Response (( string ) $response -> getBody (), $response -> getStatusCode (), $response -> getHeaders ());
2015-08-17 17:00:26 -07:00
}
}