function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jshjsh 

Programatic access to Reports

RE: http://sfdc-heretic.warped-minds.com/2006/04/10/progmatic-access-to-salesforcecom-reports/

Does anyone have PHP example code to read the report list, then execute the report and get csv results into a variable?

Thanks!

ClaiborneClaiborne

Code:

<—php
        // Retrieves a list of SFDC reports available to the current user
  // $endpoint and $sessionId are returned when you log into salesforce.com
    function GetSFReportList($endPoint, $sessionId){
        $reportUrl = $endPoint."/servlet/servlet.ReportList–sessionId=".$sessionId;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $reportUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

            // Not doing any verification of SSL certificates
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

        curl_setopt($ch, CURLOPT_TIMEOUT, 300);
        $result = curl_exec ($ch);
        curl_close ($ch);

            // Parse the XML result into an array
        $reportList = explode("\n", $result);

            // Remove first two rows and last row
        $reportList = array_slice($reportList, 2, count($reportList) - 3);

        return $reportList;
    }

        // Retrieve a CSV report
    function GetReportCSV($endPoint, $sessionId, $reportId) {
            // The endpoint url stored with the session does not
            // support the GET function. Use the url below instead.
        $url = substr($endPoint, 0, strpos($endPoint, ".com") + 4);

        $reportUrl = $url."/".$reportId."˜export=1&enc=UTF-8&xf=csv";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $reportUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

            // Not doing any verification of SSL certificates
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

        curl_setopt($ch, CURLOPT_COOKIE, 'sid='.$sessionId);
        setcookie("sid", $sessionId, 0, "/", ".salesforce.com", 0);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        $result = curl_exec ($ch);
        curl_close ($ch);

            // Parse the XML result into an array
        $reportCSV = explode("\n", $result);

            // Remove first two rows and last rows
        $reportCSV = array_slice($reportCSV, 0, count($reportCSV) - 8);

        return $reportCSV;
    }
™>


 
This works for me:

 

wernerolmwernerolm
Hello,

i have a problem trying to acces the reports with your method. When I use your code i can't access the reports.
So... i changed the way of handling the sessionID -> cookie. Trying to access the reports in this way, i always get "Session Timed Out.".
I have no idea what i am doing wrong.

Propobly the way i use your function is wrong. Can you show me an working example, please?

I also looked for a different way for accesing the reports, but i did not found anything.

Thank you

Code:
<—php
/* ... */
GetSFReportList('https://na5.salesforce.com', $connection->getSessionID());

function GetSFReportList($endPoint, $sessionId){
        $reportUrl = $endPoint."/servlet/servlet.ReportList";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $reportUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_COOKIE, 'sid='.$sessionID);

            // Not doing any verification of SSL certificates
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

        curl_setopt($ch, CURLOPT_TIMEOUT, 300);
        $result = curl_exec ($ch);
        curl_close ($ch);

        echo "<pre>";
        print_r($result);
        echo "</pre>";
    }

?>

 



Message Edited by wernerolm on 08-04-2008 05:14 AM
ClaiborneClaiborne
Sorry to take so long to reply. I was on vacation, and then Gustav hit (I live in New Orleans).

But all is well.

The code in my original example used an undocumented call to the salesforce.com api. By being undocumented, that meant that salesforce.com could change it at any point and not tell anyone.

I do not know if it has changed since I developed the original code, over 2 years ago.

You might try contact Scott Hemmeter. His web site is www.arrowpointe.com. His map application is well integrated with the salesforce.com reporting system.