现在的位置: 首页 > 综合 > 正文

几中POST请求

2017年12月26日 ⁄ 综合 ⁄ 共 927字 ⁄ 字号 评论关闭

方式一:

$url = 'http://website/a.php';
$fields = array(
'UserName'=>urlencode('a'),
'PWD'=>urlencode('b') ,
'AppReturn'=>urlencode('c') ,
'AppSQL'=>urlencode('d') ,
);
$fields_string = http_build_query($fields);
$ch = curl_init() ;
curl_setopt($ch, CURLOPT_URL,$url) ; //设置要采集的URL
curl_setopt($ch, CURLOPT_POST,1) ; //设置形式为POST  
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string) ;//设置Post参数
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);  //用字符串打印出来。
$re = curl_exec($ch);//(如果没有设置CURLOPT_RETURNTRANSFER为1,那么将会执行并获取HTML文档内容,如:1;否则将会将结果返回,而不是执行)
print_r($re);
$info = curl_getinfo($ch);//请求的有关信息
print_r($info);

方式二:

$data = array ('UserName' => 'bar');
$data = http_build_query($data);
$opts = array (
    'http' => array (
        'method' => 'POST',
        'header'=> "Content-type: application/x-www-form-urlencoded\r\n" .
                   "Content-Length: " . strlen($data) . "\r\n",
        'content' => $data
    ),
);
$context = stream_context_create($opts);
$html = file_get_contents($url, false, $context);
echo $html;
var_dump($http_response_header);

方式三:

简单的表单请求

抱歉!评论已关闭.