Integrations and APIs for Service

Get Involved. Join the Conversation.

Topic

    Pathaksa Tongpitak
    Exposing File Attachment URLAnswered
    Topic posted September 16, 2011 by Pathaksa TongpitakSilver Medal: 2,000+ Points, last edited February 8, 2012 
    5812 Views, 31 Comments
    Title:
    Exposing File Attachment URL
    Content:

    I'm trying to fetch the File Attachment URL of a Contact but it returns an empty value.

    However I do get a value when I fetch the ID.

    Am I missing something?

    Code Snippet:

    Best Comment

    Ryan McCullough

    Note in the documentation:

    http://community.rightnow.com/developer/fileexchange/Connect_PHP_November_2010/Content/How%20Do%20I/File%20Attachments/Download%20a%20File%20Attachment.htm

    That it says:

    NoteIn order to use the getAdminURL() method the initConnectAPI() has to be called using a valid username/password. For example: initConnectAPI('connect', 'connect');

    Comment

    • Mark Rhoads

      If it is a FileAttachment with Private=true, a FileAttachment on an Incident, or any FileAttachment that does not have the Private property on it, then only the method getAdminURL() will return a URL.

      E.g.:
      echo $oContact ->FileAttachments[0]->getAdminURL();

       

    • Pathaksa Tongpitak

      I also tried that. It does not work either. Any other possibilities?

    • Mark Rhoads

      Are file attachments on other objects giving you the same problem?

    • Pathaksa Tongpitak

      Yes, the same problem applies to Incidents.

    • Mark Rhoads

       Is ConnectPHP throwing any exceptions?

    • Pathaksa Tongpitak

      ConnectPHP is not throwing any exceptions.

      I can read the ID and FileName fields.

    • Ryan McCullough

      Note in the documentation:

      http://community.rightnow.com/developer/fileexchange/Connect_PHP_November_2010/Content/How%20Do%20I/File%20Attachments/Download%20a%20File%20Attachment.htm

      That it says:

      NoteIn order to use the getAdminURL() method the initConnectAPI() has to be called using a valid username/password. For example: initConnectAPI('connect', 'connect');

    • Pathaksa Tongpitak

      Hi Ryan,

      That works excellent for me. I get a response now Cool

      Thank you very much!

    • Pathaksa Tongpitak

      Now I try to read the file.

      Strangely enough, the following code displays no content of the file:

      --

      use RightNow\Connect\v1_1 as RNCPHP;
      require_once( get_cfg_var("doc_root")."/ConnectPHP/Connect_init.php" );

      initConnectAPI('username', 'password');
      $oContact = RNCPHP\Contact::fetch(1);
      $sURL =  $oContact->FileAttachments[0]->getAdminURL();
      $sContent = file_get_contents($sURL);

      echo $sURL;

      --

      However, I can paste the URL in the browser and it lets me download the file but that's not what I want.

      I tried to run this code on both the Customer Portal and the External Events directory.

    • Ryan McCullough

      I suspect the URL returned by getAdminURL() starts with https. file_get_contents() does not support https, and I have heard it reported that replacing the https with http in the URL doesn't work.

      I may have a solution for this, but I want Mark Rhoads to take a look at it first.

    • Pathaksa Tongpitak

      Yeah, I was already suspecting it has something to do with the https.

      I could bypass it with a dirty trick by fetching an external phpscript on a server outside RightNow which itself fetches this https URL using a parameter but I prefer a clean method to keep it better maintainable.

    • Pathaksa Tongpitak

      Since the Customer Portal does not allow file_get_contents with URLs on the same server to prevent infinite loops you need to use a Bypass script like this:

      #################################
      // load RightNow Connect API
      use RightNow\Connect\v1_1 as RNCPHP;
      require_once( get_cfg_var("doc_root")."/ConnectPHP/Connect_init.php" );

      // mandatory authorization for getAdminURL function
      initConnectAPI('username', 'password');

      // fetch Incident Object
      $iIncidentID = 1;
      $oIncident = RNCPHP\Incident::fetch($iIncidentID);

      // get Admin URL of first attachment
      $sURL = $oIncident->FileAttachments[0]->getAdminURL();

      // pass the URL to an external script to fetch the contents of the URL
      $sEncodedURL = base64_encode($sURL);
      $sContent = file_get_contents('http://www.yourserver.com/plugins/rightnow/bypass_file_get_contents.php?url='.$sEncodedURL);
      #################################

       

      You will also need an external script hosted on a server other than RightNow to fetch the actual contents of the File Attachment:

      <?php
      #################################
      #bypass_file_get_contents.php source code
      #################################

      $sURL = base64_decode($_REQUEST['url']);
      echo file_get_contents($sURL);
      ?>

       

    • Diane Kohnert

      Hi there,

      Are there any other solutions to getting the contents of an Incident file attachment, other than to use a non-RightNow server?

      We are having this issue too, I just need to see the contents of the Incident attachment to look for a value.


      Thanks for any help you can provide,

      Diane

    • Allan Schrum

      Hi Diane,

      Can you explain your use case? I suspect there are other ways to convey this information than just using a file attachment. In any case it would help us with deciding how to handle these situations.

      Thanks,

      -Allan

    • Diane Kohnert

      Hi Allan,

      Thanks for your reply.

      We want to create a customization for a client off a incident create event.  

      Here is the scenario:  the customer has a SPAM identification system that creates an email that gets sent to RN and this becomes an incident with an attachment.   Our code will take that newly created incident and look into the contents of the attachment and identify other needed fields (ie -an IP address where the spam originated).  We will then update the incident with the other fields (ie - IP Address) to a custom field off the incident, and put the content of the attachment into a note.  This will allow our client to create a report to identify groups of IP addresses, etc...

      I thought I would be able to do this as shown in the RN Connect documentation's sample code for FileAttachments, but I also am getting empty contents when trying to retrieve the file attachment data.

      Any idea would be greatly appreciated.

      Thanks!
      Diane