PHP Classes

curl non-callable safe version

Recommend this page to a friend!

      PHP oAuth2 lightweight wrapper  >  All threads  >  curl non-callable safe version  >  (Un) Subscribe thread alerts  
Subject:curl non-callable safe version
Summary:Some hosts disable curl, a simple mod fixes this
Messages:1
Author:Nigel Johnson
Date:2014-01-11 13:54:30
 

  1. curl non-callable safe version   Reply   Report abuse  
Picture of Nigel Johnson Nigel Johnson - 2014-01-11 13:54:30
Admittedly, I've only tested this in PHP 5.4, but you can use streams instead of cURL...

public function curl_request($url, $method, $postvals) {
if (is_callable ( 'curl_init' )) {
$ch = curl_init ( $url );
if ($method == "POST") {
$options = array (
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $postvals,
CURLOPT_RETURNTRANSFER => 1
);
} else {

$options = array (
CURLOPT_RETURNTRANSFER => 1
);
}
curl_setopt_array ( $ch, $options );
if ($this->header) {

curl_setopt ( $ch, CURLOPT_HTTPHEADER, array (
$this->header . $postvals
) );
}

$response = curl_exec ( $ch );
curl_close ( $ch );
// print_r($response);
return $response;
} else {
$options = array (
"http" => array (
"method" => $method
)
);

if ($method == "POST" && strlen ( @$postvals )) {
$options["http"]["content"] = $postvals;
}
if ($this->header) {
$options["http"]["header"] = $this->header . $postvals;
}
$context = stream_context_create ( $options );
$result = file_get_contents ( $url, false, $context );

if ($result === false) {
return false;
}

return $result;
}
}