php中模拟POST传递数据的两种方法分享
作者:bea
方法1 代码如下: $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://192.168.1.135/turntable/get_jump.php"); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_POSTFIELDS, $array); curl_exec($ch); curl_close($ch); 方法2 代
方法1
代码如下:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://192.168.1.135/turntable/get_jump.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $array);
curl_exec($ch);
curl_close($ch);
方法2
代码如下:
$data['uid'] = $this->uid;
$data['efforts'] = $res['efforts'];
$data['breakthrough'] = $res['breakthrough'];
$data['target'] = $res['target'];
$str = '';
foreach ($data as $k=>$v) {
if (is_array($v)) {
foreach ($v as $kv => $vv) {
$str .= '&' . $k . '[' . $kv . ']=' . urlencode($vv);
}
} else {
$str .= '&' . $k . '=' . urlencode($v);
}
}
$context =
array('http' =>
array('method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded'."
".
'User-Agent: Manyou API PHP Client 0.1 (non-curl) '.phpversion()."
".
'Content-length: ' . strlen($str),
'content' => $str));
$contextid = stream_context_create($context);
$sock = fopen('http://192.168.1.135/turntable/get_jump.php', 'r', false, $contextid);
if ($sock) {
$result = '';
while (!feof($sock)) {
$result .= fgets($sock, 4096);
}
fclose($sock);
有用 | 无用
代码如下:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://192.168.1.135/turntable/get_jump.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $array);
curl_exec($ch);
curl_close($ch);
方法2
代码如下:
$data['uid'] = $this->uid;
$data['efforts'] = $res['efforts'];
$data['breakthrough'] = $res['breakthrough'];
$data['target'] = $res['target'];
$str = '';
foreach ($data as $k=>$v) {
if (is_array($v)) {
foreach ($v as $kv => $vv) {
$str .= '&' . $k . '[' . $kv . ']=' . urlencode($vv);
}
} else {
$str .= '&' . $k . '=' . urlencode($v);
}
}
$context =
array('http' =>
array('method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded'."
".
'User-Agent: Manyou API PHP Client 0.1 (non-curl) '.phpversion()."
".
'Content-length: ' . strlen($str),
'content' => $str));
$contextid = stream_context_create($context);
$sock = fopen('http://192.168.1.135/turntable/get_jump.php', 'r', false, $contextid);
if ($sock) {
$result = '';
while (!feof($sock)) {
$result .= fgets($sock, 4096);
}
fclose($sock);
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- linux系统上支持php的 iconv()函数的方法
- php中mysql模块部分功能的简单封装
- php XMLWriter类的简单示例代码(RSS输出)
- 简单的PHP缓存设计实现代码
- php利用iframe实现无刷新文件上传功能的代码
- php json_encode奇怪问题说明
- PHP安全配置详细说明
- 使用PHP遍历文件夹与子目录的函数代码
- 新浪微博API开发简介之用户授权(PHP基础篇)
- PHP+MYSQL会员系统的登陆即权限判断实现代码
- PHP将DateTime对象转化为友好时间显示的实现代码
- php方法调用模式与函数调用模式简例
- php若干单维数组遍历方法的比较
- PHP学习笔记 用户注册模块用户类以及验证码类
- PHP无刷新上传文件实现代码
- PHP下利用shell后台运行PHP脚本,并获取该脚本的Process ID的代码
- php去除重复字的实现代码
- PHP与SQL注入攻击防范小技巧
- php中判断字符串是否全是中文或含有中文的实现代码