欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP get和post向服务器发送请求

程序员文章站 2023-01-22 23:33:28
1 .get请求 2.post请求 ......

1 .get请求

  

<?php 
//请求url地址

$token="xxx";
$url = "请求的地址";
//初始化curl
$ch = curl_init($url);
//3.设置参数
curl_setopt($ch,curlopt_returntransfer,1);
//4.调用接口
$res = curl_exec($ch);
if(curl_errno($ch)){
    var_dump(curl_error($ch));
}
$resarr = json_decode($res,1);
var_dump($resarr);
//5.关闭curl
curl_close($ch);




 ?>

2.post请求

<?php
    
    /*
     * $url post请求地址
     * $rawdata post参数
     */
    function curl_post_raw($url,$rawdata){
        $ch = curl_init();
        curl_setopt($ch,curlopt_url,$url);
        curl_setopt($ch,curlopt_header,0);
        curl_setopt($ch,curlopt_returntransfer,1);
        curl_setopt($ch,curlopt_connecttimeout,10);
        curl_setopt($ch,curlopt_post,1);
        curl_setopt($ch,curlopt_ssl_verifypeer,false);
        curl_setopt($ch, curlopt_postfields, $rawdata);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

//这里获取获取到的token
$token="26_-2fvbef4qoad3y37c-saxcjtaj2xvjiu1nyptgjszvbfsfl0nth9bi1abdxnyc3ywhwcmq5tfbvofb58qmyg0_wrboeczyyo1xeepqkezoyki623gqwv73jmmvhtf6vmodgi-j_bdheh3enrwxhcafapqa";
//字符串转化
$phone = "1000000000000";
$time_out = time(); 
//需要发送请求的数据 $params = json_encode( array( 'contact'=>array( 'phone'=>$phone, 'time_out'=>$time_out ) ) ); $url = "https://api.weixin.qq.com/card/invoice/setbizattr?action=set_contact&access_token={$token}"; $aa=curl_post_raw($url,$params); var_dump($aa); ?>