0 Replies Latest reply on Nov 12, 2012 8:53 AM by Anthony Krinsky

    csv export to PHP

    Anthony Krinsky

      Here's a challenge for somebody very smart out there.

       

      I’m trying to pipe the contents of a file into PHP.  The request gets redirected a few times and I’m trying to pass down the following headers:


      Cookie: workgroup_session_id

      Cookie: tableau_online_id

      P3P


      Passing all of them doesn’t work… Passing these alone, doesn’t work.  Here’s the PHP page which is not working:

       

      <html>

      <head>

      <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">

      <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">

      <title>Data Extract</title>

      </head>

      <body

          style="padding: 0px; margin: 0px; border: 0px none; width: 100%; height: 100%;">

          <?php

       

          $user=$_GET["user"];

          $wgserver=$_GET["host_url"];

          $target=$_GET["url"];

          $remote_addr=$_SERVER['REMOTE_ADDR'];

          $params = array(

                  'username' => $user,

                  'client_ip' => $remote_addr

          );

       

          $trustedPos = strrpos($target,"/trusted/");

       

          if ($trustedPos == false){

              $ticket=http_parse_message(http_post_fields($wgserver."/trusted", $params))->body;

              if ($ticket==-1){

                  //echo "Could not get ticket: ".$ticket."<br>";

              } else {

                  $viewPos = strrpos($target,"/views/");

                  $targetNew=substr ($target,0,$viewPos)."/trusted/".$ticket."".substr ($target,$viewPos);

                  //echo "Final target url: ".$targetNew."<br>";

                  $target=$targetNew;

              }

          }

       

       

          if (strpos($target, ".csv")>0){

              // First check response headers

              $headers = get_headers($target);        

              //workgroup_session_id

              //tableau_online_id

              $workgroup_session_id_header = "";

              $tableau_online_id_header = "";

              $p3p_header="";

              $allHeaders="";

       

              // Test for 301 or 302

              if(preg_match('/^HTTP\/\d\.\d\s+(301|302)/',$headers[0]))

              {

                  foreach($headers as $value)

                  {

                      if(substr(strtolower($value), 0, 9) == "location:")

                      {

                          $furl = trim(substr($value, 9, strlen($value)));

       

                      }

                  }

              }        

              

              foreach($headers as $value) {

                  if(strrpos($value,"workgroup_session_id")>-1)

                  {

                      $workgroup_session_id_header=$value;

                  }

                  if(strrpos($value,"tableau_online_id")>-1)

                  {

                      $tableau_online_id_header=$value;

                  }

                  if(strrpos($value,"P3P")>-1)

                  {

                      $p3p_header=$value;

                  }

                  

                  $allHeaders.=$value."<hr>";

              }

              

              $authenticationHeader = $workgroup_session_id_header."\r\n".$tableau_online_id_header."\r\n".$p3p_header."\r\n";

              

              

              echo "authenticationHeader:<p>".$authenticationHeader."<hr>";

              // Set final URL

              $furl = ($furl) ? $furl : $target;

       

              $opts = array(

                      'http'=>array(

                              'method'=>"GET",

                              'header'=> $authenticationHeader

                      )

              );        

              $context = stream_context_create($opts);

              echo "After following redirects, URL: ".$furl."<hr>";

              $content = file_get_contents($furl, true, $context);

              echo $content;

       

          } else {

              echo "Tableau URL did not contain .csv";

          }

       

          ?>

      </body>

      </html>