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

前端之家收集整理的这篇文章主要介绍了php中模拟POST传递数据的两种方法分享前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

方法1
<div class="codetitle"><a style="CURSOR: pointer" data="9502" class="copybut" id="copybut9502" onclick="doCopy('code9502')"> 代码如下:

<div class="codebody" id="code9502">
$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
<div class="codetitle"><a style="CURSOR: pointer" data="1216" class="copybut" id="copybut1216" onclick="doCopy('code1216')"> 代码如下:
<div class="codebody" id="code1216">
$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);

原文链接:https://www.f2er.com/php/27615.html

猜你在找的PHP相关文章