curl api的使用例子
以下是一个使用curl API的示例代码:
```php
namespace curl;
class ApiClient
{
//请求的token
const token = 'token_str';
//请求url
private $url;
param name //请求的类型
private $requestType;
//请求的数据
private $data;
//curl实例
private $curl;
public $status;
private $headers = array();
public function __construct($url, $data = array(), $requestType = 'get')
{
// url是必须要传的,并且是符合PATHINFO模式的路径
if (!$url) {
return false;
}
$this->requestType = strtolower($requestType);
$paramUrl = '';
// PATHINFO模式
if (!empty($data)) {
foreach ($data as $key => $value) {
$paramUrl .= $key . '=' . $value.'&';
}
$url = $url .'?'. $paramUrl;
$this->url = $url;
$this->data = $data;
try {
if (!$this->curl = curl_init()) {
throw new Exception('curl初始化错误:');
}
} catch (Exception $e) {
echo 'print_r($e->getMessage());';
echo '';
}
curl_setopt($this->curl, CURLOPT_URL, $this->url);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
}
}
//设置get请求的参数
public function _get()
{
}
//设置post请求的参数
public function _post()
{
curl_setopt($this->curl, CURLOPT_POST, 1);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->data);
}
//设置put请求
public function _put()
{
curl_setopt
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论