HTTP PUT方法实例
2014-04-04 11:18
176 查看
1.test.php:
data.php:
RC4.php:
<?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:
<?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:
<?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 <mjsabby@gmail.com> * @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); } } ?>
相关文章推荐
- TCP协议分析
- 关于android主线程不能访问网络异常NetworkOnMainThreadException
- ASIHTTPRequest详解
- 【复习】再谈httpurlconnection
- Java网络编程之网络基础
- 常用网络设置
- windows 内核情景分析 --- 网络通信
- 使用AVAudioPlayer播放网络音乐
- UNIX网络编程--服务器的几种异常终止
- WP8中使用async/await扩展HttpWebRequest
- HTTP条件Get(Conditional Get)
- 已知ip地址和其子网掩码如何求网络号子网号主机号
- Linux内核源代码分析——fork()原理&多进程网络模型
- 径向基网络(RBF network)之BP监督训练
- c linux socket http get
- 怎么在linux下用c或是其他语言 构造一个http(get)请求呢?
- cocos2dx网络传输之CURL
- TCP/IP三次握手与四次挥手
- Linux下MySQL常用操作网络汇总总结
- 20140403 TCPL 第一章 致读者