100 lines
3.0 KiB
PHP
100 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace phpcommon;
|
|
|
|
class HttpClient {
|
|
|
|
public static function post($url, $postarray, &$response)
|
|
{
|
|
$ch = curl_init();
|
|
try{
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $postarray);
|
|
$response = curl_exec($ch);
|
|
return true;
|
|
}catch(Exception $e){
|
|
return false;
|
|
}
|
|
curl_close($ch);
|
|
}
|
|
|
|
public static function upload($url, $filedata, &$response)
|
|
{
|
|
$ch = curl_init();
|
|
try{
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
#curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $filedata);
|
|
$response = curl_exec($ch);
|
|
return true;
|
|
}catch(Exception $e){
|
|
return false;
|
|
}
|
|
curl_close($ch);
|
|
}
|
|
|
|
public static function postContent($url, $content, &$response, $headers = null,
|
|
$ssl_key = null, $ssl_cert = null)
|
|
{
|
|
$ch = curl_init();
|
|
if (!$headers) {
|
|
$headers = array(
|
|
);
|
|
}
|
|
try{
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
|
|
if (!empty($ssl_key) && !empty($ssl_cert)) {
|
|
curl_setopt($ch, CURLOPT_SSLKEY, $ssl_key);
|
|
curl_setopt($ch, CURLOPT_SSLCERT, $ssl_cert);
|
|
}
|
|
$response = curl_exec($ch);
|
|
return true;
|
|
}catch(Exception $e){
|
|
return false;
|
|
}
|
|
curl_close($ch);
|
|
}
|
|
|
|
public static function get($url, $getarray, &$response)
|
|
{
|
|
$ch = curl_init();
|
|
try{
|
|
$getfields = '';
|
|
foreach($getarray as $key => $value){
|
|
$getfields .= '&' . $key . '=' . urlencode($value);
|
|
}
|
|
if($getfields != ''){
|
|
if(strstr($url, '?')){
|
|
$url .= $getfields;
|
|
}else{
|
|
$url .= '?' . $getfields;
|
|
}
|
|
}
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
curl_setopt($ch, CURLOPT_HTTPGET, 1);
|
|
$response = curl_exec($ch);
|
|
return true;
|
|
}catch(Exception $e){
|
|
return false;
|
|
}
|
|
curl_close($ch);
|
|
}
|
|
|
|
}
|
|
|
|
?>
|