Friday, June 12, 2020

Php script for pinging another server with curl function!

Php script for pinging another server with curl function!




Here is a simple solution for the developers who wants to ping another server they own. for example if i have two servers and namely one is admin server and another one is my client server. from admin server i want to fetch all the images that i upload to the client server.Here we need two files one resides in server for the curl and another one for the client server.




The server file

The server file contains the image upload queries and the action for the client server call


<form action="server.php?action" enctype="multipart/form-data" method="GET">
<label>upload file in server</label>
<input type="file" name="image_upload">
<input type="submit" name="submit">
</form>
<?php
if (isset($_GET['action'])) {
$file=getimagesize($_FILES['image_upload']['tmp_name']);//get the image size for checking the file type and size with the tag name
	//check the mime type
$mimeType = $file['mime'];// here i have called the variable file to get the mime type of it
 $allowedMimeTypes = ['image/jpeg','image/jpg','image/png', 'image/gif','image/bmp','image/webp'];   
 //here i have created the allowed mime type that i want
     //now let's check if the mime type matches or not
  if (!in_array($mimeType, $allowedMimeTypes)){
    	header('location:server.php?error');
    //this will redirect to the server page where if you create             the error condition
    	//or u can just echo 'error';
    }else{
    	//do the uploading thing here
    	$path='image/';
    	if (!is_dir($path.'upload')) {
    //make directory if not exists
        mkdir($path.'upload',0777,true);
        $directory=$path.'upload';
        $filename =str_replace(' ', '_',  $_FILES['image_upload']['name']);
            //to eliminate white space i do like this by adding underscore
        move_uploaded_file($_FILES["image_upload"]["tmp_name"],                 $directory.'/'.$filename);
 //file moved to server directory..now initiate the curl to call the client server file
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                httpGet('https://www.my_client_server/fetch_image_by_name.php?name='.$filename);
    	}else{
//if directory exists then do this part for just uploading
   $directory=$path.'upload';
  $filename =str_replace(' ','_',$_FILES['image_upload']['name']);
  //to eliminate white space i do like this by adding underscore
 move_uploaded_file($_FILES["image_upload"]["tmp_name"], $directory.'/'.$filename);
 //file moved to server directory..now initiate the curl to call the client server file
                      httpGet('https://www.my_client_server/fetch_image_by_name.php?name='.$filename);
    	}
}
//the curl function that i have called is here
function httpGet($url){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	//remove this if u don't want bcz it will work same 
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_TIMEOUT, 80);
	$response = curl_exec($ch);
	if(curl_error($ch)){
	  echo 'Request Error:' . curl_error($ch);
	}
	else
	{
	  echo $response;
	}
	curl_close($ch);
}
?>
Now we have to create the client server file that will fetch the image form server.The file will be



fetch_image_by_name.php

we have created file in this name because the server file calls this file with the image name we have just uploded in server.here this file will get the image form the server with the name that we have just passed from server.




if(isset($HTTP_GET_VARS['name'])){
 //checking if in this header location  whether there is a file named with the passed name
$file_headers=@get_headers('http://www.admin_server.com/ images/uploads' .$HTTP_GET_VARS['name']);
if($file_headers[0] != 'HTTP/1.1 404 Not Found'){
//checking if error not found occurs
$filesize = str_replace('Content-Length: ','',$file_headers[6]);
//this is gives us the file size $image_name=$HTTP_GET_VARS['name'];
if($filesize > 50){
 if(!file_exists('images/'.$image_name)){                
                movefile('http://www.admin_server.com/images/uploads'.$image_name,$image_name);
 }elseif(filesize('image' .$image_name) < 50){
 //the images folder here is client server image folder      
               movefile('http://www.admin_server.com/images/uploads'.$image_name,$image_name);
//takes image from server where we have added the image    
           }
        }
    }
}
if(isset($HTTP_GET_VARS['delete'])){//like the file name we can also pass the old file name if uploading new file or deleting the existing file in server
     $image_name=$HTTP_GET_VARS['delete'];
     if(file_exists('images/'.$image_name)){ 
        unlink('images/'.$image_name);
     }
}
//here the move file is the function that takes the image from the server folder and checkes the image type and converts it to the webp file type and stores in the client server.

function movefile($filename, $new_filename = '') {
    if($filename == ''){
    return 'Geen plaatje gevonden.';    
    }
    $s_filename = 'crons2/watermark.png';
    $type = strtolower(substr(strrchr($filename,"."),1));
if($type == 'jpeg') $type = 'jpg';
switch($type){
    case 'bmp': $im = imagecreatefromwbmp($filename); break;
    case 'gif': $im = imagecreatefromgif($filename); break;
    case 'jpg': $im = imagecreatefromjpeg($filename); break;
    case 'png': $im = imagecreatefrompng($filename); break;
    default : return "Unsupported picture type!";
  }
imagejpeg($im,'images/'. $new_filename,'100');
if ($type == 'jpg') {
    imagewebp($im, 'images/'. str_replace('.jpg','.webp',$new_filename),'100');
}elseif ($type == 'png') {
    imagewebp($im, 'images/'. str_replace('.png','.webp',$new_filename),'100');
}elseif ($type == 'bmp') {
    imagewebp($im, 'images/'. str_replace('.bmp','.webp',$new_filename),'100');
}elseif ($type == 'gif') {
    imagewebp($im, 'images/'. str_replace('.gif','.webp',$new_filename),'100');
}
}

1 comment:

Anonymous said...

Great Job friend, it's very useful...Keep Going

Complete web-development for beginners

Personal Home Page-PHP When we talk about php we should know where we use the php and what is the purpose of it. The php we use mainly for t...