有没有人使用过这个Highrise API PHP Wrapper库?我需要帮助验证

时间:2011-05-24 21:20:00

标签: php api authentication wrapper highrise

所以我从这个github链接下载了一个包装类:

https://github.com/ignaciovazquez/Highrise-PHP-Api

我只想尝试任何回应。到目前为止,我甚至无法使用我的凭据进行身份验证,因此我想知道是否有任何使用该API的人可以帮助我。

我尝试在终端上运行其中一个没有参数的测试文件,这就是它告诉我的:

Usage: php users.test.php [account-name] [access-token]

好吧,所以决定拿到我的证件。所以这就是我理解的,如果我错了,请纠正:

帐户名是您的高层帐户的网址中的部分。所以如果你的网址是:

https://exampleaccount.highrisehq.com/

然后您的帐户名称是:“exampleaccount”

您的访问令牌是您的身份验证令牌,您可以点击我的信息> Highrise帐户中的API令牌。

是吗?

嗯,无论如何,我输入此信息并且脚本终止时出现致命错误并显示以下消息:

Fatal error: Uncaught exception 'Exception' with message 'API for User returned Status Code: 0 Expected Code: 200' in /Users/me/Sites/sandbox/PHP/highrise_api_class/lib/HighriseAPI.class.php:137
Stack trace:
#0 /Users/me/Sites/sandbox/PHP/highrise_api_class/lib/HighriseAPI.class.php(166): HighriseAPI->checkForErrors('User')
#1 /Users/me/Sites/sandbox/PHP/highrise_api_class/test/users.test.php(13): HighriseAPI->findMe()
#2 {main}
  thrown in /Users/me/Sites/sandbox/PHP/highrise_api_class/lib/HighriseAPI.class.php on line 137

我已经完成了n00b,我真的不明白它在说什么,所以我想知道是否有任何帮助。非常感谢。

测试脚本的来源(users.test.php)是:

<?php
require_once("../lib/HighriseAPI.class.php");

if (count($argv) != 3)
    die("Usage: php users.test.php [account-name] [access-token]\n");

$hr = new HighriseAPI();
$hr->debug = false;
$hr->setAccount($argv[1]);
$hr->setToken($argv[2]);

print "Finding my user...\n";
$user = $hr->findMe();
print_r($user);

print "Finding all users...\n";
$users = $hr->findAllUsers();
print_r($users);

?>

和Highrise API包装器文件(Highrise.API.class)的源是:

<?php

    /*
        * http://developer.37signals.com/highrise/people
        *
        * TODO LIST:
        * Add Tasks support
        * Get comments for Notes / Emails
        * findPeopleByTagName
        * Get Company Name, etc proxy
        * Convenience methods for saving Notes $person->saveNotes() to check if notes were modified, etc.
        * Add Tags to Person
    */

    class HighriseAPI
    {
        public $account;
        public $token;
        protected $curl;
        public $debug;

        public function __construct()
        {
            $this->curl = curl_init();
            curl_setopt($this->curl,CURLOPT_RETURNTRANSFER,true);

        curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Accept: application/xml', 'Content-Type: application/xml'));
            // curl_setopt($curl,CURLOPT_POST,true);
            curl_setopt($this->curl,CURLOPT_SSL_VERIFYPEER,0);
            curl_setopt($this->curl,CURLOPT_SSL_VERIFYHOST,0);  
        }

        public function setAccount($account)
        {
            $this->account = $account;
        }

        public function setToken($token)
        {
            $this->token = $token;
            curl_setopt($this->curl,CURLOPT_USERPWD,$this->token.':x');
        }

        protected function postDataWithVerb($path, $request_body, $verb = "POST")
        {
            $this->curl = curl_init();

            $url = "https://" . $this->account . ".highrisehq.com" . $path;

            if ($this->debug)
                print "postDataWithVerb $verb $url ============================\n";


            curl_setopt($this->curl, CURLOPT_URL,$url);
            curl_setopt($this->curl, CURLOPT_POSTFIELDS, $request_body);
            if ($this->debug == true)
                curl_setopt($this->curl, CURLOPT_VERBOSE, true);

            curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Accept: application/xml', 'Content-Type: application/xml'));
          curl_setopt($this->curl, CURLOPT_USERPWD,$this->token.':x');
            curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER,0);
            curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST,0);
            curl_setopt($this->curl, CURLOPT_RETURNTRANSFER,true);


            if ($verb != "POST")
              curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $verb);
            else
                curl_setopt($this->curl, CURLOPT_POST, true);

            $ret = curl_exec($this->curl);

            if ($this->debug == true)
                print "Begin Request Body ============================\n" . $request_body . "End Request Body ==============================\n";

            curl_setopt($this->curl,CURLOPT_HTTPGET, true);

            return $ret;
        }

        protected function getURL($path)
        {
            curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Accept: application/xml', 'Content-Type: application/xml'));
          curl_setopt($this->curl, CURLOPT_USERPWD,$this->token.':x');
            curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER,0);
            curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST,0);
            curl_setopt($this->curl, CURLOPT_RETURNTRANSFER,true);

            $url = "https://" . $this->account . ".highrisehq.com" . $path;

            if ($this->debug == true)
                curl_setopt($this->curl, CURLOPT_VERBOSE, true);


            curl_setopt($this->curl,CURLOPT_URL,$url);
            $response = curl_exec($this->curl);

            if ($this->debug == true)
                print "Response: =============\n" . $response . "============\n";

            return $response;

        }

        protected function getLastReturnStatus()
        {
            return curl_getinfo($this->curl, CURLINFO_HTTP_CODE); 
        }

        protected function getXMLObjectForUrl($url)
        {
            $xml = $this->getURL($url);
            $xml_object = simplexml_load_string($xml);
            return $xml_object;
        }

        protected function checkForErrors($type, $expected_status_codes = 200)
        {
            if (!is_array($expected_status_codes))
                $expected_status_codes = array($expected_status_codes);

            if (!in_array($this->getLastReturnStatus(), $expected_status_codes))
            {
                switch($this->getLastReturnStatus())
                {
                    case 404:
                        throw new Exception("$type not found");
                        break;
                    case 403:
                        throw new Exception("Access denied to $type resource");
                        break;
                    case 507:
                        throw new Exception("Cannot create $type: Insufficient storage in your Highrise Account");
                        break;

                    default:
                        throw new Exception("API for $type returned Status Code: " . $this->getLastReturnStatus() . " Expected Code: " . implode(",", $expected_status_codes));
                        break;
                }               
            }
        }

        /* Users */

        public function findAllUsers()
        {
            $xml = $this->getUrl("/users.xml");
            $this->checkForErrors("User");

            $xml_object = simplexml_load_string($xml);

            $ret = array();
            foreach($xml_object->user as $xml_user)
            {
                $user = new HighriseUser();
                $user->loadFromXMLObject($xml_user);
                $ret[] = $user;
            }

            return $ret;
        }

        public function findMe()
        {
            $xml = $this->getUrl("/me.xml");
            $this->checkForErrors("User");

            $xml_obj = simplexml_load_string($xml);
            $user = new HighriseUser();
            $user->loadFromXMLObject($xml_obj);
            return $user;
        }

        /* Tasks */

        public function findCompletedTasks()
        {
            $xml = $this->getUrl("/tasks/completed.xml");
            $this->checkForErrors("Tasks");
            return $this->parseTasks($xml);
        }

        public function findAssignedTasks()
        {
            $xml = $this->getUrl("/tasks/assigned.xml");
            $this->checkForErrors("Tasks");
            return $this->parseTasks($xml);
        }


        public function findUpcomingTasks()
        {
            $xml = $this->getUrl("/tasks/upcoming.xml");
            $this->checkForErrors("Tasks");
            return $this->parseTasks($xml);
        }

        private function parseTasks($xml)
        {
            $xml_object = simplexml_load_string($xml);          
            $ret = array();
            foreach($xml_object->task as $xml_task)
            {
                $task = new HighriseTask($this);
                $task->loadFromXMLObject($xml_task);
                $ret[] = $task;
            }

            return $ret;

        }

        public function findTaskById($id)
        {
            $xml = $this->getURL("/tasks/$id.xml");
            $this->checkForErrors("Task");
            $task_xml = simplexml_load_string($xml);
            $task = new HighriseTask($this);
            $task->loadFromXMLObject($task_xml);
            return $task;

        }

        /* Notes & Emails */

        public function findEmailById($id)
        {
            $xml = $this->getURL("/emails/$id.xml");
            $this->checkForErrors("Email");
            $email_xml = simplexml_load_string($xml);
            $email = new HighriseEmail($this);
            $email->loadFromXMLObject($email_xml);
            return $email;
        }

        public function findNoteById($id)
        {
            $xml = $this->getURL("/notes/$id.xml");
            $this->checkForErrors("Note");
            $note_xml = simplexml_load_string($xml);
            $note = new HighriseNote($this);
            $note->loadFromXMLObject($note_xml);
            return $note;
        }

        public function findPersonById($id)
        {
            $xml = $this->getURL("/people/$id.xml");

            $this->checkForErrors("Person");


            $xml_object = simplexml_load_string($xml);

            $person = new HighrisePerson($this);
            $person->loadFromXMLObject($xml_object);
            return $person;
        }

        public function findAllTags()
        {
            $xml = $this->getUrl("/tags.xml");
            $this->checkForErrors("Tags");

            $xml_object = simplexml_load_string($xml);          
            $ret = array();
            foreach($xml_object->tag as $tag)
            {
                $ret[(string)$tag->name] = new HighriseTag((string)$tag->id, (string)$tag->name);
            }

            return $ret;
        }

        public function findAllPeople()
        {
            return $this->parsePeopleListing("/people.xml");    
        }

        public function findPeopleByTagName($tag_name)
        {
            $tags = $this->findAllTags();
            foreach($tags as $tag)
            {
                if ($tag->name == $tag_name)
                    $tag_id = $tag->id;
            }

            if (!isset($tag_id))
                throw new Excepcion("Tag $tag_name not found");

            return $this->findPeopleByTagId($tag_id);
        }

        public function findPeopleByTagId($tag_id)
        {
            $url = "/people.xml?tag_id=" . $tag_id;
            $people = $this->parsePeopleListing($url);
            return $people; 
        }

        public function findPeopleByEmail($email)
        {
         return $this->findPeopleBySearchCriteria(array("email"=>$email));
        }

        public function findPeopleByTitle($title)
        {
            $url = "/people.xml?title=" . urlencode($title);

            $people = $this->parsePeopleListing($url);
            return $people;
        }



        public function findPeopleByCompanyId($company_id)
        {
            $url = "/companies/" . urlencode($company_id) . "/people.xml";
            $people = $this->parsePeopleListing($url);
            return $people;
        }

        public function findPeopleBySearchTerm($search_term)
        {
            $url = "/people/search.xml?term=" . urlencode($search_term);
            $people = $this->parsePeopleListing($url, 25);
            return $people;
        }

        public function findPeopleBySearchCriteria($search_criteria)
        {
            $url = "/people/search.xml";

            $sep = "?";
            foreach($search_criteria as $criteria=>$value)
            {
                $url .= $sep . "criteria[" . urlencode($criteria) . "]=" . urlencode($value);
                $sep = "&";
            }

            $people = $this->parsePeopleListing($url, 25);
            return $people;
        }

        public function findPeopleSinceTime($time)
        {
            $url = "/people/search.xml?since=" . urlencode($time);
            $people = $this->parsePeopleListing($url);
            return $people;
        }
        public function parsePeopleListing($url, $paging_results = 500)
        {
            if (strstr($url, "?"))
                $sep = "&";
            else
                $sep = "?";

            $offset = 0;
            $return = array();
            while(true) // pagination
            {
                $xml_url = $url . $sep . "n=$offset";
                // print $xml_url;
                $xml = $this->getUrl($xml_url);
                $this->checkForErrors("People");
                $xml_object = simplexml_load_string($xml);

                foreach($xml_object->person as $xml_person)
                {
                    // print_r($xml_person);
                    $person = new HighrisePerson($this);
                    $person->loadFromXMLObject($xml_person);
                    $return[] = $person;
                }

                if (count($xml_object) != $paging_results)
                    break;

                $offset += $paging_results;
            }

            return $return;
        }

    }

很抱歉这是一个很长的文件,但如果它有帮助,那就这样吧。

编辑:所以我想我得到了它的工作。我应该说我试图在我的本地服务器上测试这个库,并且由于某种原因它会继续失败但是当我将脚本移动到Rackspace云上的开发服务器时它会起作用。这让我很困惑。两个服务器都支持PHP curl,所以我无法真正理解问题所在。

编辑:我不确定两种服务器配置之间的区别是什么,但无论如何这里是我的curl配置的两个服务器的phpinfo函数输出的几个屏幕截图:

Localhost服务器:

enter image description here

和rackspace云服务器:

enter image description here

3 个答案:

答案 0 :(得分:2)

API的分支...... https://github.com/AppSaloon/Highrise-PHP-Api ...似乎更发达,维护得更好。

不是提供答案,而是更好的起点。

答案 1 :(得分:1)

啊,因为实际上没有HTTP错误代码0,我希望您的请求不会发送到Highrise的网站,或者您没有正确地将帐户名称和令牌传递给该类。你能列出users.test.php班的来源吗?

编辑:测试了课程和你的代码,它对我有用。您可能要么错误地复制了库文件,要么将您的令牌复制错误。

答案 2 :(得分:0)

我有同样的问题。我肯定有错误的帐户。我有https://foo.highrisehq.com而不是foo