Archive

Posts Tagged ‘http’

HTTP Class

January 5th, 2009

Class buat GET dan POST pake PHP, mengsimpelkan pemanggilan ke website laen, bisa buat REST juga

<?
  if (!extension_loaded("curl")) dl("curl.so"); // USed for MWN
 
  function HTTPPost($data , $url , $referer) {
    $ch = curl_init();
    $proxy = getOneProxy();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Set supaya return value nya ke string
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_REFERER, $referer);
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
    curl_setopt($ch, CURLOPT_POST, 1);
//    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
//    curl_setopt($ch, CURLOPT_PROXY, $proxy);
//    echo "Using proxy $proxy\n";
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $retval = curl_exec($ch);
//    echo $retval;
    curl_close($ch);
  }
 
  function HTTPGet($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Set supaya return value nya ke string
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_REFERER, "");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
    $retval = curl_exec($ch);
    return $retval;
    curl_close($ch);
  }
 
  function doREST($url, $params) {
    if ($params) {
      foreach($params as $key => $param) {
        $_par[] = "$key=" . urlencode($param);
      }
      $_complete = "?";
      $_complete .= implode("&" , $_par);
    }
    return HTTPGet($url . $_complete);
  }
 
 
 
?>
Share

web , , , ,

Switch to our mobile site