Calling REST via PHP

Hello,

The REST API documetation is a bit lite on the PHP examples. So, I thought it might useful to start a thread where we can compile a few examples to help others out and hopefully save some time. Below are two examples making call to the REST API via PHP. Both are tested and work fine. Both examples are calling the method, CRM/Constituents/{constituentId}. Please, make sure to update the login, password, and URL if you choose to try it. I will post a ready to go version in my files so you can download it. When I get some more time I will add some examples using "PUT" and  some local procedure to populate/update a local table. Hope it helps.

Travis

Example 1 cURL:

$login = 'YourUserName:UserGroup:MachineName';
$password = 'TessituraPassword';
$url = 'https://SomeDomain.tessituranetworkramp.com/TestAPI/TessituraService/CRM/Constituents/{constituentId}'; // replace {constituentId} = a consituent number, example 70865
$method = 'GET';  
$data = false;

/*******************************************************************************
cURL function

http://php.net/manual/en/book.curl.php
********************************************************************************/
function CallRESTapi($method,$url,$login,$password,$data)

{
    switch ($method)
    {
        case "POST":
            curl_setopt($curl, CURLOPT_POST, 1);
            if ($data)
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            break;
        case "PUT":
            curl_setopt($curl, CURLOPT_PUT, 1);
            break;
        default:
            if ($data)
                $url = sprintf("%s?%s", $url, http_build_query($data));
    }
    
    
    
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_USERPWD,"$login:$password");
    curl_setopt($curl, CURLOPT_HTTPGET, 1);

    $results = curl_exec($curl);

    curl_close($curl);
    
    return $results;

}

//call the function
$fnc_data= CallRESTapi($method,$url,$login,$password,$data);
echo $fnc_data;

 

Example 2 file_get_contents:

/*******************************************************************************
file_get_contents

http://php.net/manual/en/function.file-get-contents.php
********************************************************************************/

$context = stream_context_create(array(
    'http' => array(
        'header'  => "Authorization: Basic " . base64_encode("$login:$password")
    )
));
$data = file_get_contents($url, false, $context);

echo $data;



[edited by: Travis Armbuster at 2:29 PM (GMT -6) on 2 May 2017]
Parents
  • Here is a quick example grabbing values from the returned JSON object.

    Using the cURL code from "Example 1" above, pass $fnc_data variable into the json_decode function. From there it is a piece of cake to step through the object a pull the information you're interested in grabbing. To test this simple append it to the cURL call.

    Example (http://php.net/manual/en/function.json-decode.php):


    $json = json_decode($fnc_data);
    $create_dt = $json->CreatedDateTime;
    $Constgroup = $json->ConstituentType->ConstituentGroup->Description;

    //show the vars
    echo 'Create Date: '.$create_dt.'<br>';
    echo 'Const Group: '.$Constgroup.'<br>';

     

    *Note: This will work for both examples you just need to adjust the var past to the json_decode() function.



    [edited by: Travis Armbuster at 2:28 PM (GMT -6) on 2 May 2017]
Reply
  • Here is a quick example grabbing values from the returned JSON object.

    Using the cURL code from "Example 1" above, pass $fnc_data variable into the json_decode function. From there it is a piece of cake to step through the object a pull the information you're interested in grabbing. To test this simple append it to the cURL call.

    Example (http://php.net/manual/en/function.json-decode.php):


    $json = json_decode($fnc_data);
    $create_dt = $json->CreatedDateTime;
    $Constgroup = $json->ConstituentType->ConstituentGroup->Description;

    //show the vars
    echo 'Create Date: '.$create_dt.'<br>';
    echo 'Const Group: '.$Constgroup.'<br>';

     

    *Note: This will work for both examples you just need to adjust the var past to the json_decode() function.



    [edited by: Travis Armbuster at 2:28 PM (GMT -6) on 2 May 2017]
Children