Skip to content

Commit

Permalink
修改curl更多方式
Browse files Browse the repository at this point in the history
  • Loading branch information
hanwenbo committed Nov 21, 2018
1 parent 1da70ae commit 88b7f80
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,47 @@ public function request( string $method, string $url, array $params = null ) : R
$request->setUserOpt( [CURLOPT_POSTFIELDS => $params['body']] );
}
break;
case 'PUT' :
if( $params && isset( $params['form_params'] ) ){
foreach( $params['form_params'] as $key => $value ){
$request->addPost( new Field( $key, $value ) );
}
} elseif( $params && isset( $params['body'] ) ){
if( !isset( $params['header']['Content-Type'] ) ){
$params['header']['Content-Type'] = 'application/json; charset=utf-8';
}
$request->setUserOpt( [CURLOPT_CUSTOMREQUEST => "PUT"] );
$request->setUserOpt( [CURLOPT_POSTFIELDS => $params['body']] );
}
break;
case 'DELETE' :
if( $params && isset( $params['form_params'] ) ){
foreach( $params['form_params'] as $key => $value ){
$request->addPost( new Field( $key, $value ) );
}
} elseif( $params && isset( $params['body'] ) ){
if( !isset( $params['header']['Content-Type'] ) ){
$params['header']['Content-Type'] = 'application/json; charset=utf-8';
}
$request->setUserOpt( [CURLOPT_CUSTOMREQUEST => "DELETE"] );
$request->setUserOpt( [CURLOPT_POSTFIELDS => $params['body']] );
}
break;
case 'PATCH' :
if( $params && isset( $params['form_params'] ) ){
foreach( $params['form_params'] as $key => $value ){
$request->addPost( new Field( $key, $value ) );
}
} elseif( $params && isset( $params['body'] ) ){
if( !isset( $params['header']['Content-Type'] ) ){
$params['header']['Content-Type'] = 'application/json; charset=utf-8';
}
$request->setUserOpt( [CURLOPT_CUSTOMREQUEST => "PATCH"] );
$request->setUserOpt( [CURLOPT_POSTFIELDS => $params['body']] );
}
break;
default:
throw new \InvalidArgumentException( "method eroor" );
throw new \InvalidArgumentException( "method error" );
break;
}

Expand Down

0 comments on commit 88b7f80

Please sign in to comment.