Categories
Posts

PHP Helpers: curl_http_request

cURL is one very handy program and library, I love having access to in PHP. It has a ton of options though, and I can never seem to remember to flip the right knobs without reviewing the options list. 99% of the time I just want a simple function that does the right thing when making an HTTP request. So here’s the new PHP Helpers function that sets up reasonable defaults for making HTTP requests using cURL:

[sourcecode lang=”php”]
if ( !function_exists( ‘curl_http_request’ ) ) {
function curl_http_request( $url, $req = ‘GET’, $arg = array( ) ) {
$body = ”;
$cookies = array( );
$err_no = 0;
$err_msg = ”;
$headers = array( );
$info = array( );

$opt = array(
‘connect_timeout’ => 5,
‘cookies’ => array( ),
‘follow_location’ => TRUE,
‘http_headers’ => array( ),
‘max_redirects’ => 5,
‘timeout’ => 15,
‘password’ => ”,
‘post_fields’ => array( ),
‘user_agent’ => ‘PHP curl_http_get’,
‘username’ => ”,
‘verify_ssl’ => TRUE
);
foreach ( $opt as $k => $v ) {
if ( isset( $arg[$k] ) ) {
$opt[$k] = $arg[$k];
}
}

$curl_opt = array(
CURLOPT_AUTOREFERER => TRUE,
CURLOPT_CONNECTTIMEOUT => $opt[‘connect_timeout’],
CURLOPT_CUSTOMREQUEST => $req,
CURLOPT_ENCODING => ”,
CURLOPT_FOLLOWLOCATION => $opt[‘follow_location’],
CURLOPT_HEADER => TRUE,
CURLOPT_MAXREDIRS => $opt[‘max_redirects’],
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_TIMEOUT => $opt[‘timeout’],
CURLOPT_USERAGENT => $opt[‘user_agent’],
);

if (
is_array( $opt[‘cookies’] )
&& count( $opt[‘cookies’] ) > 0
) {
$curl_opt[CURLOPT_COOKIE] = implode( ‘;’, $opt[‘cookies’] );
}

if ( !empty( $opt[‘username’] ) ) {
$curl_opt[CURLOPT_USERPWD] = $opt[‘username’] . ‘:’ . $opt[‘password’];
}

if (
is_array( $opt[‘post_fields’] )
&& count( $opt[‘post_fields’] ) > 0
) {
$curl_opt[CURLOPT_POST] = TRUE;
$curl_opt[CURLOPT_POSTFIELDS] = $opt[‘post_fields’];
}

if ( $req == ‘HEAD’ ) {
$curl_opt[CURLOPT_NOBODY] = TRUE;
}

if (
is_array( $opt[‘http_headers’] )
&& count( $opt[‘http_headers’] ) > 0
) {
$curl_opt[CURLOPT_HTTPHEADER] = $opt[‘http_headers’];
}

if ( $opt[‘verify_ssl’] === FALSE ) {
$curl_opt[CURLOPT_SSL_VERIFYPEER] = FALSE;
$curl_opt[CURLOPT_SSL_VERIFYHOST] = 0;
}

$curl = curl_init( $url );
curl_setopt_array( $curl, $curl_opt );

$body = curl_exec( $curl );
$err_no = curl_errno( $curl );
$err_msg = curl_error( $curl );
$info = curl_getinfo( $curl );
curl_close( $curl );

$header_string = trim( substr( $body, 0, $info[‘header_size’] ) );
$body = substr( $body, $info[‘header_size’] );

if ( strpos( $header_string, "rnrn" ) !== FALSE ) {
$header_string = end( explode( "rnrn", $header_string ) );
$header_string = str_replace( "rn", "n", $header_string );
}

foreach ( explode( "n", $header_string ) as $line ) {
list( $k, $v ) = explode( ‘:’, $line, 2 );
if ( empty( $v ) ) {
continue;
}

if ( strtolower( $k ) == ‘set-cookie’ ) {
$cookies[] = trim( $v );
} else {
$headers[$k] = trim( $v );
}
}

return array(
‘body’ => $body,
‘err_no’ => $err_no,
‘err_msg’ => $err_msg,
‘headers’ => $headers,
‘cookies’ => $cookies,
‘info’ => $info
);
} // function curl_http_request
}
[/sourcecode]

It’s a bit longer that then other PHP Helper functions 🙂 .

4 replies on “PHP Helpers: curl_http_request”

[php]require( ‘wp-load.php’ );

function curl_http_request( $url, $req = ‘GET’, $arg = array( ) ) {
$arg[‘method’] = $req;
return wp_remote_request( $url, $arg );
}[/php]

/whistle

Lies!

I actually personally often end up doing stuff like that for non-WordPress scripts of mine. I’ve been spoiled by all of WordPress’ helper functions, heh. 🙂

haha!

Agreed, it’s easy to take for granted the number of things that WordPress provides. But that’s why these posts were called ‘PHP Helpers’ and not ‘WordPress Helpers’ 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *