php http put,HTTP PUT方法实例

1.test.php:

require_once('inc/class/RC4.php');

function curl_request($url,$data,$method='POST'){

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //设为TRUE把curl_exec()结果转化为字串,而不是直接输出

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //设置请求方式

curl_setopt($ch,CURLOPT_HTTPHEADER,array("X-HTTP-Method-Override: $method"));//设置HTTP头信息

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$document = curl_exec($ch);

curl_close($ch);

return $document;

}

$rc4 = new rc4crypt();

$url = 'http://localhost/test/data.php';

$data = json_encode(array('id'=>1), true);

$pwd = 'my_key';

$encrypted_data = $rc4->encrypt($pwd, $data);

$return = curl_request($url, base64_encode($encrypted_data), 'PUT');

var_dump($return);

data.php:

require_once('inc/class/RC4.php');

error_reporting(E_ERROR);

$_PUT = array();

if($_SERVER['REQUEST_METHOD'] == 'PUT') {

parse_str(file_get_contents("php://input"), $_PUT);

foreach($_PUT as $key => $val) {

$data = $key;

}

$rc4 = new rc4crypt();

$pwd = 'mykey';

$encryted_data = base64_decode($data);

$json_data = $rc4->decrypt($pwd, $encryted_data);

$data = json_decode($json_data, true);

//print_r($data);

$id = $data['id'];

echo $id;

}

RC4.php:

/* vim: set expandtab shiftwidth=4 softtabstop=4 tabstop=4: */

/**

* RC4Crypt 3.2

*

* RC4Crypt is a petite library that allows you to use RC4

* encryption easily in PHP. It's OO and can produce outputs

* in binary and hex.

*

* (C) Copyright 2006 Mukul Sabharwal [http://mjsabby.com]

* All Rights Reserved

*

* @link http://rc4crypt.devhome.org

* @author Mukul Sabharwal

* @version $Id: class.rc4crypt.php,v 3.2 2006/03/10 05:47:24 mukul Exp $

* @copyright Copyright © 2006 Mukul Sabharwal

* @license http://www.gnu.org/copyleft/gpl.html

* @package RC4Crypt

*/

/**

* RC4 Class

* @package RC4Crypt

*/

class rc4crypt {

/**

* The symmetric encryption function

*

* @param string $pwd Key to encrypt with (can be binary of hex)

* @param string $data Content to be encrypted

* @param bool $ispwdHex Key passed is in hexadecimal or not

* @access public

* @return string

*/

static function encrypt ($pwd, $data, $ispwdHex = 0)

{

if ($ispwdHex)

$pwd = @pack('H*', $pwd); // valid input, please !

$key[] = '';

$box[] = '';

$cipher = '';

$pwd_length = strlen($pwd);

$data_length = strlen($data);

for ($i = 0; $i < 256; $i++)

{

$key[$i] = ord($pwd[$i % $pwd_length]);

$box[$i] = $i;

}

for ($j = $i = 0; $i < 256; $i++)

{

$j = ($j + $box[$i] + $key[$i]) % 256;

$tmp = $box[$i];

$box[$i] = $box[$j];

$box[$j] = $tmp;

}

for ($a = $j = $i = 0; $i < $data_length; $i++)

{

$a = ($a + 1) % 256;

$j = ($j + $box[$a]) % 256;

$tmp = $box[$a];

$box[$a] = $box[$j];

$box[$j] = $tmp;

$k = $box[(($box[$a] + $box[$j]) % 256)];

$cipher .= chr(ord($data[$i]) ^ $k);

}

return $cipher;

}

/**

* Decryption, recall encryption

*

* @param string $pwd Key to decrypt with (can be binary of hex)

* @param string $data Content to be decrypted

* @param bool $ispwdHex Key passed is in hexadecimal or not

* @access public

* @return string

*/

static function decrypt ($pwd, $data, $ispwdHex = 0)

{

return rc4crypt::encrypt($pwd, $data, $ispwdHex);

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值