Home >Backend Development >PHP Tutorial >Share two methods of simulating POST data transfer in PHP_PHP Tutorial

Share two methods of simulating POST data transfer in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:24:57753browse

Method 1

Copy code The code is as follows:

$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);

Method 2
Copy code The code is as follows:

$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'."rn".
'User-Agent: Manyou API PHP Client 0.1 (non-curl) '.phpversion()."rn".
' 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);

www.bkjia.comtruehttp: http: //192.168.1.135/turntable/get_jump.php"); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt(...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn