반응형

1. POST 메소드
<?php
    $auth_param = "인증 파라미터"; //인증 Key 값
    $content = "변수A=x&변수B=y"; //Form 값
    $target_ip="xxx.xxx.xxx.xxx"; //서버 IP
    $target_port=yyyy; //서버 포트
    $target_uri="POST URI"; //요청 URI
 
    $http_header = array();
    $http_header[] = 'POST ' . $target_uri . ' HTTP/1.1';
    $http_header[] = 'Content-Type: application/x-www-form-urlencoded';
    $http_header[] = 'Host: ' . $target_ip . ':' . $target_port;
    $http_header[] = 'Authorization: Basic '.$auth_param;
    $http_header[] = 'Content-Length: ' . strlen($content);
    $http_header[] = 'Connection: close';
    $message = implode("\r\n",$http_header) . "\r\n\r\n" . $content;

    echo "HTTP_MESSAGE:[".$message."]\n\n";
    $fp = fsockopen($target_ip,$target_port,$errno,$errstr,10);
    if(!get_resource_type($fp)=='stream') { return false;}
    if(!fwrite($fp,$message)){
        fclose($fp);
        echo "HTTP_SEND ERROR\n";
        exit;
    }
    $http_response=array();
    while(!feof($fp)){
        $http_response[]=fgets($fp,4096);
    }
    fclose($fp);

    $http_res_code=substr($http_response[0],9,3);
    $http_res_message=substr($http_response[0],13);

    if($http_res_code=="200")
    {
        echo "POST Success:\n";
        for($i=0;$i<count($http_response);$i++){
            if($http_response[$i]=="\r\n") {
                $http_result=$http_response[$i+1];
            }
        }
        echo "RETURNED Resource:".$http_result."\n";
    }
    else
    {
        echo "POST Failed:\n";
        echo "ERROR CODE:".$http_res_code."\n";
        echo "ERROR MESSAGE:".$http_res_message."\n";
    }
?>

2. PUT 메소드
<?php
    $auth_param = "인증 파라미터"; //인증 Key 값
    $target_ip="xxx.xxx.xxx.xxx"; //서버 IP
    $target_port=yyyy; //서버 포트
    $target_uri="PUT URI"; //요청 URI

    $http_header = array();
    $http_header[] = 'PUT ' . $target_uri . ' HTTP/1.1';
    $http_header[] = 'Host: ' . $target_ip . ':' . $target_port;
    $http_header[] = 'Authorization: Basic '.$auth_param;
    $http_header[] = 'Accept: */*';
    $http_header[] = 'Content-Length: 0';
    $message = implode("\r\n",$http_header) . "\r\n\r\n";

    echo "HTTP_MESSAGE:[".$message."]\n\n";

    $fp = fsockopen($target_ip,$target_port,$errno,$errstr,10);

    if(!get_resource_type($fp)=='stream') { return false;}
    if(!fwrite($fp,$message)){
        fclose($fp);
        echo "HTTP_SEND ERROR\n";
        exit;
    }
    $http_response=array();
    while(!feof($fp)){
        $http_response[]=fgets($fp,4096);
    }
    fclose($fp);

    $http_res_code=substr($http_response[0],9,3);
    $http_res_message=substr($http_response[0],13);

    if($http_res_code=="200")
    {
        echo "PUT Success:\n";
    }
    else
    {
        echo "PUT Failed:\n";
        echo "ERROR CODE:".$http_res_code."\n";
        echo "ERROR MESSAGE:".$http_res_message."\n";
    }
?>

3. GET 메소드
<?php
    $auth_param = "인증 파라미터"; //인증 Key 값
    $target_ip="xxx.xxx.xxx.xxx"; //서버 IP
    $target_port=yyyy; //서버 포트
    $target_uri="GET URI"; //요청 URI

    $http_header = array();
    $http_header[] = 'GET ' . $target_uri . ' HTTP/1.1';
    $http_header[] = 'Host: ' . $target_ip . ':' . $target_port;
    $http_header[] = 'Authorization: Basic '.$auth_param;
    $http_header[] = 'Accept: */*';
    $message = implode("\r\n",$http_header) . "\r\n\r\n";

    echo "HTTP_MESSAGE:[".$message."]\n\n";

    $fp = fsockopen($target_ip,$target_port,$errno,$errstr,10);
    if(!get_resource_type($fp)=='stream') { return false;}
    if(!fwrite($fp,$message)){
        fclose($fp);
        echo "HTTP_SEND ERROR\n";
        exit;
    }

    $http_response=array();
    while(!feof($fp)){
        $http_response[]=fgets($fp,4096);
    }
    fclose($fp);

    $http_res_code=substr($http_response[0],9,3);
    $http_res_message=substr($http_response[0],13);

    if($http_res_code=="200")
    {
        echo "GET Success:\n";
        for($i=0;$i<count($http_response);$i++){
            if($http_response[$i]=="\r\n") {
                $http_result=$http_response[$i+1];
            }
        }
        echo "RETURNED Resource:".$http_result."\n";
    }
    else
    {
        echo "GET Failed:\n";
        echo "ERROR CODE:".$http_res_code."\n";
        echo "ERROR MESSAGE:".$http_res_message."\n";
    }
?>

4. DELETE 메소드
<?php
    $auth_param = "인증 파라미터"; //인증 Key 값
    $target_ip="xxx.xxx.xxx.xxx"; //서버 IP
    $target_port=yyyy; //서버 포트
    $target_uri="GET URI"; //요청 URI

    $http_header = array();
    $http_header[] = 'DELETE ' . $target_uri . ' HTTP/1.1';
    $http_header[] = 'Authorization: Basic '.$auth_param;
    $http_header[] = 'Host: ' . $target_ip . ':' . $target_port;
    $http_header[] = 'Accept: */*';

    $message = implode("\r\n",$http_header) . "\r\n\r\n";

    echo "HTTP_MESSAGE:[\n".$message."\n]\n\n";

    $fp = fsockopen($target_ip,$target_port,$errno,$errstr,10);
    if(!get_resource_type($fp)=='stream') { return false;}

    if(!fwrite($fp,$message)){
        fclose($fp);
        echo "HTTP_SEND ERROR\n";
        exit;
    }

    $http_response=array();
    while(!feof($fp)){
        $http_response[]=fgets($fp,4096);
    }
    fclose($fp);

    $http_res_code=substr($http_response[0],9,3);
    $http_res_message=substr($http_response[0],13);

    if($http_res_code=="200")
    {
        echo "DELETE Success:\n";
        for($i=0;$i<count($http_response);$i++){
            if($http_response[$i]=="\r\n") {
                $http_result=$http_response[$i+1];
            }
        }
        echo "RETURNED Resource:".$http_result."\n";
    }
    else
    {
        echo "DELETE Failed:\n";
        echo "ERROR CODE:".$http_res_code."\n";
        echo "ERROR MESSAGE:".$http_res_message."\n";
    }
?>

반응형
Posted by alias
,