如何让我的API和一个随机密钥生成从第三方URL进入我的控制器在CodeIgniter


How to get my API and a Random keygen from a third party URL into my Controller in CodeIgniter

我在CI中构建了一个应用程序。这里我需要来自第三方网站/URL的API代码。我不知道如何将该代码接收到我的控制器中。

$user_email = "jondoe@appsapi.com.au"

Example - https://www.sample.com.au/api/apps/auth{$user_email}

当我在浏览器中输入这个Example域时,它提供了一个API密钥。比如-A9D5w9pL如何将其输入控制器。

控制器-

public function auth_with_api() {
    //https://www.sample.com.au/api/apps/auth{$user_email}
    // here I require the API key. How can I receive it here.
}

您可以通过cURL来实现。只需从这里下载codeIgniter cURL库

http://getsparks.org/packages/curl/show (死链路)

https://github.com/philsturgeon/codeigniter-curl(更新链接-Git Repo)

将此库文件放在libraries文件夹中。

所以现在在控制器-

public function auth_with_api() 
{
    $this->load->library('curl');
    $user_email = "jondoe@appsapi.com.au"
    $api_key    = $this->curl->simple_get('https://www.sample.com.au/api/apps/auth{$user_email}'); // $api_key now get the value A9D5w9pL
    // now you can use this $api_key in this controller.
}


编辑(另一种方式)-

现在您可以使用Guzzle。它是一个PHP-HTTP客户端,它使发送HTTP请求变得简单,而与web服务集成则变得微不足道。

请查看Guzzle文档-http://docs.guzzlephp.org

public function auth_with_api() 
{
    $user_email = "jondoe@appsapi.com.au"
    $client     = new 'GuzzleHttp'Client();
    $resposne   = $client->request('GET', 'https://www.sample.com.au/api/apps/auth{$user_email}');
    $api_key    = $resposne->getBody();
    // $api_key now get the value A9D5w9pL
}

如果你有任何问题。请告诉我。