在宁静的codeigniter中使用cURL

时间:2016-10-22 07:00:03

标签: php codeigniter curl

在本机PHP中,我有一个像这样的消费宁静服务器:

$url = "http://localhost/pln/api/json?rayon=$rayon&id_pel=$id_pel&nama=$nama";

$client = curl_init($url);

curl_setopt($client,CURLOPT_RETURNTRANSFER,true);

$respone = curl_exec($client);

$result = json_decode($respone);

使用CodeIgniter时如何访问这样的cURL?

4 个答案:

答案 0 :(得分:1)

CodeIgniter 3.x周围没有活动的cURL库。有one for CI 2.x不再维护。

考虑使用非常受欢迎的Guzzle,并将其视为PHP的事实上的HTTP接口库。以下是文档中的用法示例:

  
$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'https://api.github.com/user', [
    'auth' => ['user', 'pass']
]);
echo $res->getStatusCode();
// "200"
echo $res->getHeader('content-type');
// 'application/json; charset=utf8'
echo $res->getBody();
// {"type":"User"...'

我还建议使用受Requests模块启发的Python Requests,并且比Guzzle更容易开始使用:

$headers = array('Accept' => 'application/json');
$options = array('auth' => array('user', 'pass'));
$request = Requests::get('https://api.github.com/gists', $headers, $options);

var_dump($request->status_code);
// int(200)

var_dump($request->headers['content-type']);
// string(31) "application/json; charset=utf-8"

var_dump($request->body);
// string(26891) "[...]"

作为CodeIgniter 3.x has support for Composer开箱即用的套餐,您可以通过编辑器轻松安装其中一个套餐并立即开始使用。

我强烈建议您不要按照Manthan Dave's answer中的建议下载“下载脚本”方式。 Composer为PHP提供了复杂的依赖管理生态系统;利用它! “下载此脚本”狗的日子已经结束了。

答案 1 :(得分:0)

  

您可以使用codeigniter的默认Curl库:

$this->load->library('curl');
$result = $this->curl->simple_get('http://example.com/');
var_dump($result);

有关详细信息,请参阅此链接: https://www.formget.com/curl-library-codeigniter/

答案 2 :(得分:0)

添加到@sepehr的答案。可以在codeigniter中以非常简单的方式配置请求库,如此处所述

https://stackoverflow.com/a/46062566/2472685

答案 3 :(得分:0)

我在codeigniter中使用了以下函数来处理curl url并且工作正常,试试看:

function request($auth, $url, $http_method = NULL, $data = NULL) {
//check to see if we have curl installed on the server 
        if (!extension_loaded('curl')) {
//no curl
            throw new Exception('The cURL extension is required', 0);
        }

//init the curl request
//via endpoint to curl
        $req = curl_init($url);
//set request headers
        curl_setopt($req, CURLOPT_HTTPHEADER, array(
            'Authorization: Basic ' . $auth,
            'Accept: application/xml',
            'Content-Type: application/x-www-form-urlencoded',
        ));

//set other curl options        
        curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($req, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($req, CURLOPT_TIMEOUT, 30);


//set http method
//default to GET if data is null
//default to POST if data is not null
        if (is_null($http_method)) {
            if (is_null($data)) {
                $http_method = 'GET';
            } else {
                $http_method = 'POST';
            }
        }

//set http method in curl
        curl_setopt($req, CURLOPT_CUSTOMREQUEST, $http_method);

//make sure incoming payload is good to go, set it
        if (!is_null($data)) {
            if (is_array($data)) {
                $raw = http_build_query($data);
            } else {
//Incase of raw xml
                $raw = $data;
            }
            curl_setopt($req, CURLOPT_POSTFIELDS, $raw);

        }

//execute curl request
        $raw = curl_exec($req);


        if (false === $raw) { //make sure we got something back
            throw new Exception(curl_error($req) . $url, -curl_errno($req));
        }

//decode the result
        $res = json_decode($raw);
        if (is_null($res)) { //make sure the result is good to go
            throw new Exception('Unexpected response format' . $url, 0);
        }

        return $res;
    }