使用PHP进行Curl API调用

时间:2018-10-24 00:12:54

标签: php api curl

我希望你一切都好。我目前正在处理的一个项目遇到了麻烦,我想问一下是否有人可以帮我看看?基本上,我想发生的事情是从Purplepass API中提取所有事件,并将其带到正在运行的网站上。你认为你可以帮我吗?

API链接:https://www.purplepass.com/api/docs.html

**我一直收到此错误:{“成功”:false,“错误”:{“原因”:“无效部分”} **

这是我的代码

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
</head>
<body>

 <?php

    // Making the API Call to Purplepass
    function callAPI($method, $url, $data){
        $curl = curl_init();

        switch ($method){
            case "POST":
                curl_setopt($curl, CURLOPT_POST, 1);
                if ($data)
                    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
                break;
        }

       // OPTIONS:
       curl_setopt($curl, CURLOPT_URL, $url);
       curl_setopt($curl, CURLOPT_HTTPHEADER, array(
          'APIKEY: 111111111111111111111',
          'Content-Type: application/json',
       ));
       curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
       // Execute:
       $result = curl_exec($curl);
       if(!$result){die("Connection Failure");}
       curl_close($curl);
       print($result);
       return $result;
    }
    // Setting up the credentials for authentication:
    $login =  array(
        "section" => "login",
        "action" => "login",
        "login_email" => "fake@email.com",
        "login_passwd" => "fakepassword"
    );
    // URL needed for API Call:
    $url = "https://www.purplepass.com/api/";
    $make_call = callAPI('POST', $url, json_encode($login));
    $response = json_decode($make_call, true);
 ?>
</body>

0 个答案:

没有答案