入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

CURL 请求外部接口 GET 和 POST

创建时间:2017-03-01 投稿人: 浏览次数:978
/**
 * GET方式请求
 * @return mixed $result
 */
protected function getData()
{

    // 取消SSL证书检验
    curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($this->curlHandle, CURLOPT_URL, $this->request_url);
    curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($this->curlHandle, CURLOPT_TIMEOUT, $this->timeout);
    curl_setopt($this->curlHandle, CURLOPT_HEADER, 0);
    $result = curl_exec($this->curlHandle);
    if (curl_errno($this->curlHandle)) {
        return false;
    } else {
        return $result;
    }
}

/**
 * POST方式请求
 * @param mixed $data 需要发送的数据
 * @return mixed $result
 */
private function postData($data)
{
    $data = json_encode($data, JSON_UNESCAPED_UNICODE);
    // 取消SSL证书检验
    curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($this->curlHandle, CURLOPT_URL, $this->request_url);
    curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($this->curlHandle, CURLOPT_POST, 1);
    curl_setopt($this->curlHandle, CURLOPT_TIMEOUT, $this->timeout);
    curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, $data);
    $result = curl_exec($this->curlHandle);

    if (curl_errno($this->curlHandle)) {
        echo curl_errno($this->curlHandle).":".curl_error($this->curlHandle);
        return false;
    } else {
        return $result;
    }
}
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
  • 上一篇:没有了
  • 下一篇:没有了
未上传头像