Get Categories for JobOrder PHP Script

Forum for users and developers of Bullhorn's Webservices API suite

Moderators: StaffingSupport, s.emmons, BullhornSupport

Post Reply
lxjam
User
Posts: 1
Joined: Wed Apr 24, 2013 1:42 am

Get Categories for JobOrder PHP Script

Post by lxjam » Wed Apr 24, 2013 2:07 am

This took me forever but I finally got there! I don't care if the PHP code is not great as after hours of trying it works!!

I hope this helps someone!

(You'll need SOAP installed on your webserver and PHP's SimpleXML extension - which I think SimpleXML is part of PHP now as standard).

Code: Select all

<?php
		// Create a new instance of the SoapClient class.
		$params = array(
			'trace' => 1,
			'soap_version' => SOAP_1_1);
		$BHclient = new SoapClient("https://api.bullhornstaffing.com/webservices-1.1/?wsdl",$params);
		
		// To run this code, you will need a valid username, password, and API key.
		$username = "xxxxxxxxxxxxx";
		$password = "xxxxxxxxxxxx";
		$apiKey = "xxxxxxxx-xxxxxxxxx-xxxxxxxxx-xxxxxxxxxx";
		
		// Start a new session
		$session_request = new stdClass();
		$session_request->username = $username;
		$session_request->password = $password;
		$session_request->apiKey = $apiKey;
		$API_session = $BHclient->startSession($session_request);
		$API_currentSession = $API_session->return;

		// Put nto a request object
		$findId = new SoapVar('PUT_YOUR_JOB_ID_HERE', XSD_INTEGER,"int","http://www.w3.org/2001/XMLSchema");
		$request_array = array (
		//new SoapHeader('http://www.qpricer.com/Services/Pricing','Identity token="123456789"',null),
		'session' => $API_currentSession,
		'entityName' => 'JobOrder',
		 'id'=>$findId,
		'associationName' => 'categories'
		);
	   // print_r($request_array);
		
		$SOAP_request = new SoapVar($request_array, SOAP_ENC_OBJECT, "getAssociationIDs", "http://query.apiservice.bullhorn.com/");
		
		// Use the query method to return the candidate ids
		try {
		$queryResult = $BHclient->getAssociationIDs($SOAP_request);
		} catch (SoapFault $fault) {
		var_dump($BHclient->__getLastRequest());
		die($fault->faultstring);
		}
			/*echo '<pre>';
    		echo "REQUEST:\n" . $BHclient->__getLastRequest();
    		echo '</pre>';*/
		
		/*echo '<pre>';
    		echo "REQUEST:\n" . $BHclient->__getLastResponse();
    		echo '</pre>';*/
		$element = $BHclient->__getLastResponse();
		
                //Get rid of the nasty parts that PHP's SimpleXML does not like-long winded buy hey....
                $xmlstr = str_replace("S:Body","SBody",$element);
		$xmlstr = str_replace("S:Envelope","SEnvelope",$xmlstr);
		$xmlstr = str_replace(' xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"',"",$xmlstr);
		$xmlstr = str_replace('ns2:getAssociationIdsResponse',"getAssociationIdsResponse",$xmlstr);
		$xmlstr = str_replace(' xmlns:ns2="http://apiservice.bullhorn.com/"',"",$xmlstr);
		$xmlstr = str_replace(' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:int"',"",$xmlstr);
		//Replace return node as it's PHP code!!
		$xmlstr = str_replace('return',"returnthis",$xmlstr);
		$xml = new SimpleXMLElement($xmlstr);
		//print_r($xml);
		//WE FINALLY GOT THE CAT ID!!!
		$catID = $xml->SBody->getAssociationIdsResponse->returnthis->ids;
		//I am presuming that if we have more than one Category Id that they would be passed as an array i.e seperated by a comma.
		$catID = explode(",",$catID);
		echo $catID[0];
	?>

s.emmons
User
Posts: 333
Joined: Wed Dec 31, 1969 8:00 pm

Re: Get Categories for JobOrder PHP Script

Post by s.emmons » Mon Apr 29, 2013 9:47 am

Thanks for posting lxjam!
Sam Emmons
Enterprise Team Lead
BULLHORN
US Support 617-478-9126

Post Reply