【问题标题】:How to get my API and a Random keygen from a third party URL into my Controller in CodeIgniter如何在 CodeIgniter 中从第三方 URL 获取我的 API 和随机密钥生成器到我的控制器中
【发布时间】:2013-09-03 12:42:05
【问题描述】:

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

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

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

当我将这个示例域输入浏览器时,它会提供一个 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.

}

【问题讨论】:

标签: php api codeigniter controller key


【解决方案1】:

您可以通过 cURL 做到这一点。只需从这里下载 codeIgniter cURL 库

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

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

将此库文件放在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

}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-27
    • 2012-01-04
    • 1970-01-01
    • 2021-01-14
    • 1970-01-01
    • 2021-04-01
    • 2022-09-23
    • 1970-01-01
    相关资源
    最近更新 更多