Page 1 of 1

What's wrong?

Posted: Tue Dec 08, 2015 5:21 pm
by jrichet
I'm new to this API, but things don't seem to work properly. Do we need to put our server url into the user panel somewhere?

I've tested most of what's covered in the API docs and every single request returns unknown or badly structured command

does anyone know what gives?

for reference here is my [private] rest consumption class. work from ->test():

Code: Select all


class Bullhorn{
    public $token;
    public $cnt;
    public $restToken;
	public $restTokenTime;
    
    function BullHorn(){
        $this->cnt=0;
        $this->getToken();
		if(isset($_SESSION['restTokenTime'])){
			$this->restTokenTime = $_SESSION['restTokenTime'];
		}
        
    }
    function curlUrl($url,$data=array()){

         $options = array(
            CURLOPT_POST           => true,
            CURLOPT_POSTFIELDS     => $data,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HEADER         => true,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_AUTOREFERER    => true,
            CURLOPT_CONNECTTIMEOUT => 120,
            CURLOPT_TIMEOUT        => 120,
        );

        $ch = curl_init( $url );
        curl_setopt_array( $ch, $options );
        $content = curl_exec( $ch );
        curl_close( $ch );
        $this->cnt++;
        if($this->cnt>=10){
            throw new Exception("More than 10 calls in one pageload.", 1234);
        }
        return $content;
    }

    function getJsonData($url,$debug=false){
        if($debug){
            echo "<div style='padding:20px;border-bottom:1px solid #ccc;'>$url</div>";
        }
            $url = trim($url);
            $html = $this->curlUrl($url);
            $data =  json_decode(substr($html, strpos($html, '{')));
            if(isset($data->errorMessage)){
                throw new Exception($data->errorMessage."\n".$url,599);
            }
        
        return $data;
    }

    function getToken(){
            $clientID     = '[cid]';
            $clientSecret = '[secret]';
            $apiUsername  = '[uname]';
            $apiPassword  = '[pass]';
            $token;
        
          667134d6ddc6&response_type=code
            $getCodeUrl = "https://auth.bullhornstaffing.com/oauth/authorize?client_id=$clientID&response_type=code"
           ."&username=$apiUsername&password=$apiPassword&action=Login";
            $codeHtml = $this->curlUrl($getCodeUrl);
    
            $loc = strpos($codeHtml,'cation: ');
            $stringDown = substr($codeHtml,$loc,  strpos($codeHtml, ' ', $loc));
            $stringDown = str_replace('cation: http://www.bullhorn.com?', '', $stringDown);
            $stringDown = str_replace('&client_id=353a983f-f410-4ad', '', $stringDown);
            $stringDown = str_replace('code=', '', $stringDown);
            $code=$stringDown;

            $getTokenUrl = "gettokenurl";
            $tokenHtml = $this->getJsonData($getTokenUrl);

            $token = $tokenHtml->access_token;
            $_SESSION['bh_token'] = $token;
            $this->token = $token;

        return $token;
    }
    
	function getRestToken(){
		if(!isset($this->restTokenTime) || time()-$this->restTokenTime >= 599 ){
		
            $a = $this->getJsonData("https://rest.bullhornstaffing.com/rest-services/login?version=2.0&access_token=".$this->token);
            $this->restUrl = $_SESSION['restUrl'] = $a->restUrl;
            $this->restToken = $_SESSION['bhRestToken'] = $a->BhRestToken;
			$this->restTokenTime = $_SESSION['restTokenTime'] = time();
			
	    }else{
			echo time() - $this->restTokenTime;
		}
	}
    
    function restCall($url){
	
		$this->getRestToken();
		$url = trim($this->restUrl.$url."&BhRestToken=".$this->restToken);
        //die($url);
        //echo "$url<br><hr>";
        $b = $this->getJsonData($url);
        return $b;
    }
    
    
    function test($page = 1){
        //do not work
      
        $url = 'find?query= ';
        $url = 'entity/Opportunity?id=1';
        $url= 'entity/Candidate?qid =8';
        $url = "myCandidates?fields=*";
        $url="search/Opportunity?fields=*&query=province:bc";
        $url='meta/Candidate?fields=*&meta=basic';
        $url='meta/Candidate?fields=*&meta=basic';
        //working
        $url='meta/Candidate?fields=*&meta=basic'; //works when I paste it. wtf?
        
        $url = "query/JobOrder?fields=*&where=isOpen=1&BhRestToken=";
        $url = "query/Opportunity?fields=*&where=isOpen=1";
        $url = 'meta/Candidate?fields=*&meta=basic';
        $url = "myCandidates?fields=*";
        $jobs = $this->restCall($url);
        
        //d1($jobs);
        return $jobs;
    }
}