php中模拟POST传递数据的两种方法分享

yipeiwu_com5年前PHP代码库
方法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'."\r\n".
'User-Agent: Manyou API PHP Client 0.1 (non-curl) '.phpversion()."\r\n".
'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);

相关文章

PHP基于phpqrcode生成带LOGO图像的二维码实例

本文实例讲述了PHP基于phpqrcode生成带LOGO图像的二维码。分享给大家供大家参考。具体如下: 这里PHP使用phpqrcode生成带LOGO图像的二维码,使用起来很方便,代码中...

PHP中strtr字符串替换用法详解

本文实例讲述了PHP中strtr字符串替换用法。分享给大家供大家参考。具体分析如下: strtr(string,from,to)或者strtr(string,array) 首先针对str...

php支持中文字符串分割的函数

str_split不支持中文,利用mb_xx函数实现个 /** * Convert a string to an array * @param string $str * @p...

php堆排序实现原理与应用方法

本文实例讲述了php堆排序实现原理与应用方法。分享给大家供大家参考。具体分析如下: 这里以php作为描述语言较详细讲解堆排序原理,因保证程序可读性,故不做优化,php程序中关于堆的一些概...

php中flush()、ob_flush()、ob_end_flush()的区别介绍

flush()、ob_flush()、ob_end_flush()三者的区别:首先,说下buffer,它是一个内存地址空间,为4096(1kb)【在php.ini配置文件中找到outpu...