วันจันทร์ที่ 28 ตุลาคม พ.ศ. 2556

PHP curl service send xml data



<?php


  $server ="http://yourservername/getservicexml.php";
  $xmldata ="<tests>
    <control>
        <module>Modulename</module>
    </control>
    <test>
        <sn>5555</sn>
        <ticket>555</ticket>
        <test_station>5555</test_station>
        <tester_name>testername</tester_name>
        <user>user</user>
        <result>F</result>
        <resource_name>resourcename</resource_name>
        <test_log>
            <![CDATA[
       Log message or your data text data field
            ]]>
        </test_log>
    </test>
</tests>
";


  function sendRequest($requestXML,$server)
   {
   // $server = 'http://127.0.0.1/exam/php_xml/receive_postxml.php';
    $headers = array(
    "Content-type: text/xml",
    "Content-length: ".strlen($requestXML),
    "Connection: close"
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $server);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 200);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $requestXML);
 // curl_setopt($ch, CURLOPT_POSTFIELDS, "?XML=TT&password=".$password."&etc=etc");
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $data = curl_exec($ch);
    echo $data;
    if(curl_errno($ch)){
        $errmes =  curl_error($ch);
        $textlog = date("d/M/Y H:i:s",time())."  ".$errmes." something went wrong..... try later\n";
        //saveaction(ODCSENDLOG,$textlog);
        echo $textlog;
        exit();
    }else{
        curl_close($ch);
    }
   
   if(!$data)
    {
     return false;
    }
    return $data;
  }

  sendRequest($xmldata,$server);

  ?>

/*------------------------------------------*/

// Received data getservicexml.php

<?php

if (!function_exists('getallheaders'))
{
    function getallheaders()
    {
           $headers = '';
       foreach ($_SERVER as $name => $value)
       {
           if (substr($name, 0, 5) == 'HTTP_')
           {
               $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
           }
       }
       return $headers;
    }
}
 $arr1 = getallheaders(); //apache_response_headers();
 $txt = "";
 foreach($arr1 as $key => $value)
 {
  $txt .= $key.":".$value."\n";
 }

 if (isset($GLOBALS["HTTP_RAW_POST_DATA"])){
    $xml = $GLOBALS["HTTP_RAW_POST_DATA"];
    echo $xml;
   
  }

 $txt .= $xml;

 $handle=fopen("testxmlheader.log","a+");
 fwrite($handle,$txt."\n");
 fclose($handle);

  function xmlhead_savelog($text)
  {
  $xmlstr="<?xml version=\"1.0\" ?>";
  $xmlstr.="<tests>";
  $xmlstr.="<control>";
  $xmlstr.="<module>SAVELOGFILE</module>";
  $xmlstr.="</control>";
  $xmlstr.=$text;
  $xmlstr.="</tests>";
  return $xmlstr;
  }

?>

วันพุธที่ 24 กรกฎาคม พ.ศ. 2556

Resize images using PHP

if ($ext =="jpg" or $ext =="jpeg") {
$ori_img = imagecreatefromjpeg($photo);
} else if ($ext =="png") {
$ori_img = imagecreatefrompng($photo);
} else if ($ext =="gif") {
$ori_img = imagecreatefromgif($photo);
}

$ori_size = getimagesize($photo);
$ori_w = $ori_size[0];
$ori_h = $ori_size[1];
  if($ori_w >720)
  {
if ($ori_w>=$ori_h) {
$new_w = 720;
$new_h = round(($new_w/$ori_w) * $ori_h);
} else {
$new_h =720;
$new_w = round(($new_h/$ori_h) * $ori_w);
}
$new_img= imagecreatetruecolor($new_w, $new_h);
imagecopyresized( $new_img, $ori_img,0,0,0,0,$new_w, $new_h,$ori_w,$ori_h);

if ($ext =="jpg" or $ext =="jpeg") {
imagejpeg($new_img,"../images/$filename");
} else if ($ext =="png") {
imagepng($new_img,"../images/$filename");
} else if ($ext =="gif") {
imagegif($new_img,"../images/$filename");
}

imagedestroy($ori_img);
imagedestroy($new_img);
}