PHP curl 實現RESTful PUT DELETE 執行個體

來源:互聯網
上載者:User

標籤:txt   text   efault   pos   url   ade   timeout   lin   content   

用戶端

client.php

<?php

//PUT

$curl_handle = curl_init ();
// Set default options.
curl_setopt ( $curl_handle, CURLOPT_URL, ‘http://my.focus.cn/test/socket.php’);
curl_setopt ( $curl_handle, CURLOPT_FILETIME, true );
curl_setopt ( $curl_handle, CURLOPT_FRESH_CONNECT, false );

curl_setopt ( $curl_handle, CURLOPT_HEADER, true );
curl_setopt ( $curl_handle, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $curl_handle, CURLOPT_TIMEOUT, 5184000 );
curl_setopt ( $curl_handle, CURLOPT_CONNECTTIMEOUT, 120 );
curl_setopt ( $curl_handle, CURLOPT_NOSIGNAL, true );
curl_setopt ( $curl_handle, CURLOPT_HEADER, true );
curl_setopt ( $curl_handle, CURLOPT_CUSTOMREQUEST, ‘PUT’ );
$aHeader[] = “Content-Type:text/xml;charset=UTF-8″;
$aHeader[] = “x-bs-ad:private”; 
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $aHeader);
$file = ‘client.php’;
$file_size = filesize($file);
$h = fopen($file,’r‘);
curl_setopt ( $curl_handle, CURLOPT_INFILESIZE, $file_size);
curl_setopt ( $curl_handle, CURLOPT_INFILE, $h);
curl_setopt ( $curl_handle, CURLOPT_UPLOAD, true );
$ret = curl_exec ( $curl_handle );
print_r($ret);

?>

DELETE 只要將

$curl_handle = curl_init ();


// Set default options.

curl_setopt ( $curl_handle, CURLOPT_URL, ‘http://my.focus.cn/test/socket.php?file=socket.txt’);

curl_setopt ( $curl_handle, CURLOPT_FILETIME, true );

curl_setopt ( $curl_handle, CURLOPT_FRESH_CONNECT, false );

curl_setopt ( $curl_handle, CURLOPT_HEADER, true );

curl_setopt ( $curl_handle, CURLOPT_RETURNTRANSFER, true );

curl_setopt ( $curl_handle, CURLOPT_TIMEOUT, 5184000 );

curl_setopt ( $curl_handle, CURLOPT_CONNECTTIMEOUT, 120 );

curl_setopt ( $curl_handle, CURLOPT_NOSIGNAL, true );

curl_setopt ( $curl_handle, CURLOPT_CUSTOMREQUEST, ‘DELETE’ );

$ret = curl_exec ( $curl_handle );

服務端

server.php

<?php

$raw_post_data = file_get_contents(‘php://input’, ‘r’);

$method = $_SERVER[‘REQUEST_METHOD‘];

if(‘PUT’ == $method)

{
    $headers = apache_request_headers();
    file_put_contents(‘socket.txt’,$raw_post_data.print_r($headers,true));

}

else if(‘DELETE’==$method)

{

       unlink($_GET[‘file‘]);

}

echo ‘<?xml version=”1.0″ encoding=”UTF-8″?><RET>OK</RET>’;

用戶端

client.php

<?php

//PUT

$curl_handle = curl_init ();
// Set default options.
curl_setopt ( $curl_handle, CURLOPT_URL, ‘http://my.focus.cn/test/socket.php’);
curl_setopt ( $curl_handle, CURLOPT_FILETIME, true );
curl_setopt ( $curl_handle, CURLOPT_FRESH_CONNECT, false );

curl_setopt ( $curl_handle, CURLOPT_HEADER, true );
curl_setopt ( $curl_handle, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $curl_handle, CURLOPT_TIMEOUT, 5184000 );
curl_setopt ( $curl_handle, CURLOPT_CONNECTTIMEOUT, 120 );
curl_setopt ( $curl_handle, CURLOPT_NOSIGNAL, true );
curl_setopt ( $curl_handle, CURLOPT_HEADER, true );
curl_setopt ( $curl_handle, CURLOPT_CUSTOMREQUEST, ‘PUT’ );
$aHeader[] = “Content-Type:text/xml;charset=UTF-8″;
$aHeader[] = “x-bs-ad:private”; 
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $aHeader);
$file = ‘client.php’;
$file_size = filesize($file);
$h = fopen($file,’r‘);
curl_setopt ( $curl_handle, CURLOPT_INFILESIZE, $file_size);
curl_setopt ( $curl_handle, CURLOPT_INFILE, $h);
curl_setopt ( $curl_handle, CURLOPT_UPLOAD, true );
$ret = curl_exec ( $curl_handle );
print_r($ret);

?>

DELETE 只要將

$curl_handle = curl_init ();


// Set default options.

curl_setopt ( $curl_handle, CURLOPT_URL, ‘http://my.focus.cn/test/socket.php?file=socket.txt’);

curl_setopt ( $curl_handle, CURLOPT_FILETIME, true );

curl_setopt ( $curl_handle, CURLOPT_FRESH_CONNECT, false );

curl_setopt ( $curl_handle, CURLOPT_HEADER, true );

curl_setopt ( $curl_handle, CURLOPT_RETURNTRANSFER, true );

curl_setopt ( $curl_handle, CURLOPT_TIMEOUT, 5184000 );

curl_setopt ( $curl_handle, CURLOPT_CONNECTTIMEOUT, 120 );

curl_setopt ( $curl_handle, CURLOPT_NOSIGNAL, true );

curl_setopt ( $curl_handle, CURLOPT_CUSTOMREQUEST, ‘DELETE’ );

$ret = curl_exec ( $curl_handle );

服務端

server.php

<?php

$raw_post_data = file_get_contents(‘php://input’, ‘r’);

$method = $_SERVER[‘REQUEST_METHOD‘];

if(‘PUT’ == $method)

{
    $headers = apache_request_headers();
    file_put_contents(‘socket.txt’,$raw_post_data.print_r($headers,true));

}

else if(‘DELETE’==$method)

{

       unlink($_GET[‘file‘]);

}

echo ‘<?xml version=”1.0″ encoding=”UTF-8″?><RET>OK</RET>’;

PHP curl 實現RESTful PUT DELETE 執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.