PHP两种HTTP请求

第一种:POST方式

function http_post_data($url,$data_string)

{
    $ch = curl_init();// 创建一个新cURL资源
    curl_setopt($ch, CURLOPT_POST, 1);//设置请求为post;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);// 添加post数据到请求中
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//支持https请求
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//curl获取页面内容或提交数据,作为变量储存,而不是直接输出。
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json; charset=utf-8', 'Content-Length:' . strlen($data_string)));
    $return_content=curl_exec($ch);// 抓取URL并把它传递给浏览器
    curl_close($ch);//关闭cURL资源,并且释放系统资源
    return $return_content;
}


$url = "http://user.jt.yoximi.com/Login/CheckLogin";//接口地址
$json_body =json_encode(Array('sessionId'=>$b,'clientId'=>$a,'sign'=>$c));//需要发送的数据

$de_json = http_post_data($url, $json_body);

第二种:GET方式

$a = $_COOKIE["u1_client"];

$b = $_COOKIE["u1_session"];
$c = $_COOKIE["u1_sign"];
$ABC = file_get_contents("http://user.jt.yoximi.com/Login/CheckLogin?sessionId=".$b."&clientId=".$a."&sign=".$c."");
$de_json = json_decode($ABC, true);

猜你喜欢

转载自blog.csdn.net/qq_41467799/article/details/81010889
今日推荐