首页 > PHP教程 > php开发知识文章

PHP使用curl函数请求api接口的方法

这篇文章主要介绍了PHP使用curl函数请求api接口的方法的相关资料,希望通过本文能帮助到大家,需要的朋友参考下

请求接口那就有了http的请求方式,PHP常见的是GET/POST两种当然还有其他的比如put等,

java那边经常用到GET/POST/PUT/DELETE等方式,请求接口当然要用到curl的相关函数了,都是看文档调试的希望大家都看文档,

封装好的相关函数

示例代码:

class curlHttp
{
private $serverhost = "https://www.yuqingqi.com"; //测试
/**
* 请求接口封装 get/post/put/delete
* access public
* @param string $url 接口地址
* @param string $params 参数
* @param string $type 类型 get/post/put/delete
* @return bool/array
*/
public function getcurldata($url, $params, $type = "get")
{
$url = $this->serverhost . $url;
$response = array();
if ($type == 'get') { //get请求
//请求头可以加其他设置
$headers = array(
'Content-type: application/json;charset=UTF-8',
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);

} elseif ($type == 'post') { //post请求
$headers = array(
'Content-type: application/json;charset=UTF-8',
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, true); //注意这几行
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); //注意这几行
//curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);

} elseif ($type == 'put') { //put请求
$headers = array(
'Content-type: application/json;charset=UTF-8',
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_PUT, true); //注意这几行
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
//curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
}
return $response;
}

//如何调用上面代码
//get方式
/**
* 查询我创建过的班级
* @param string $url 接口地址
* @param string $params 参数
* @param string $type 类型 get
* @return array
*/
public function mycreateclass($userid)
{
$url = "/xxx/xxxx/xxxx/" . $userid; //请求地址拼接
$response = $this->getcurldata($url, array(), "get");
$createdclass = json_decode($response, true); //返回json格式数据
return $createdclass;
}

/** post方式请求
* 用户登录判断
* access public
* @param string $username 用户名
* @param string $password 密码
* @return bool
*/
public function getlogin($username, $password)
{
//post的数据
$params = array(
"username" => $username,
"password" => $password
);
$params = json_encode($params, 64 | 256);
$uri = "/xxx/xxx/login";
$response = $this->getcurldata($uri, $params, "post");
$result = json_decode($response, true);
return $result;
}

/*身份转换--put 请求
*/
public function changeuserole($token)
{
//put的数据
$params = array();
$params = json_encode($params, 64 | 256);
$uri = "/xxx/xxx/xxx/" . $token . "/";
$response = $this->getcurldata($uri, $params, "put");
$result = json_decode($response, true);
//dump($result);die;
return $result;
}
}

以上就是本文PHP使用curl函数请求api接口的方法的全部内容,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

关闭
感谢您的支持,我会继续努力!
扫码打赏,建议金额1-10元


提醒:打赏金额将直接进入对方账号,无法退款,请您谨慎操作。