Skip to content

Commit

Permalink
Clean-up Http class
Browse files Browse the repository at this point in the history
Changed:
- Use booleans intead of integers for cURL options.
- Compile URL in GET request earlier to organize cURL options similarly to POST request.
  • Loading branch information
mcaskill committed Mar 16, 2023
1 parent 7d3aa9e commit 0b1c8af
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ class Http {
public function post( $url = '', $args = array() ) {
$curl_handle = curl_init();
curl_setopt( $curl_handle, CURLOPT_URL, $url );
curl_setopt( $curl_handle, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $curl_handle, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl_handle, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $curl_handle, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $curl_handle, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt( $curl_handle, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $curl_handle, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt( $curl_handle, CURLOPT_CUSTOMREQUEST, 'POST' );
if ( ! empty( $args ) ) {
curl_setopt( $curl_handle, CURLOPT_POSTFIELDS, http_build_query( $args, '', '&' ) );
}

$response = curl_exec( $curl_handle );
curl_close( $curl_handle );

Expand All @@ -44,15 +45,15 @@ public function post( $url = '', $args = array() ) {
* @return mixed
*/
public function get( $url = '', $args = array() ) {
$query_string = '';
if ( ! empty( $args ) ) {
$url .= '?' . http_build_query( $args, '', '&' );
}

$curl_handle = curl_init();
curl_setopt( $curl_handle, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $curl_handle, CURLOPT_URL, $url );
curl_setopt( $curl_handle, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl_handle, CURLOPT_FOLLOWLOCATION, true );
if ( ! empty( $args ) ) {
$query_string = http_build_query( $args, '', '&' );
}
curl_setopt( $curl_handle, CURLOPT_URL, $url . '?' . $query_string );

$response = curl_exec( $curl_handle );
curl_close( $curl_handle );

Expand Down

0 comments on commit 0b1c8af

Please sign in to comment.