Integrations and APIs for Service

Get Involved. Join the Conversation.

Topic

    Mitchell Welch
    Incident File Attachment Access in ConnectPHP
    Topic posted January 30, 2013 by Mitchell Welch 
    2268 Views, 28 Comments
    Title:
    Incident File Attachment Access in ConnectPHP
    Content:

    Hi All,

    I have been having some trouble accessing file attachments on incident object using the connectPHP API version 1.2 in the November '12 version of RN. Basically what I need to do is access a CSV file that is attached to an incident object and do some processing based upon the contents of the file. I have been trying to use the sample code for accessing incident object attachments from the Connect documentation shown below:

     

    $incident = RNCPHP\Incident::fetch();
    for($i = 0; $i <= (count($incident->FileAttachments) - 1); $i++)
    {
    $url = $incident->FileAttachments[$i]->getAdminURL();
    $file_contents = file_get_contents($url);
    }
    

    In my situation, I only have one file attached to the incident, so the for loop is irrelevant. This code works fine up until the file_get_contents($url) call. When executed, the script hangs for a long time and then FALSE is retruned from the call into the $file_contents variable, instead of the contents of the CSV file.

    I have checked the value returned in the getAdminURL() (i.e. what should be the url to the file) call by running it in a web browser and the file is returned as a download in the web browser as expected. I have tried a few different appraoches including using fopen() directly and using the cUrl library functions to explicitly make GET requests to the url, with the user-agent parameters set in the header (I have run into situations in the past where GET and POST requests are ignored without the user agent parameter set in the hearder)

    Has anyone else experienced anything like this and does anyone have any ideas?

    Kind regards,

    Mitch 

    Version:
    November 2012

    Comment

    • Ryan McCullough

      Mitch,

      Note that file_get_contents does not support https URLs.

      If the URL is not https, then you may need to set the Host in the HTTP header like this:

      $context = array(
          'http' => array(
              'timeout' => $timeout,
              'header' => "Host: {$hostname}\r\n"
          )
      );
      $response = @file_get_contents($url, 0, stream_context_create($context));
      

      You can probably get the host from the URL returned by getAdminURL() by running it through parse_url.

    • Mitchell Welch

      Thanks Ryan,

      But still no luck. Below is the code I tried:

      $url = $incident->FileAttachments[0]->getAdminURL(); 
      $hostname = parse_url($url, PHP_URL_HOST);
      $timeout = 60000;
      $context = array(
         'http' => array(
             'timeout' => $timeout,
             'header' => "Host: {$hostname}\r\n"
              )
      );
      $fileContents = @file_get_contents($url, 0, stream_context_create($context));
      

      Unfortunately I get the same results as before; it just hangs and returns false to the $fileContents.

      Any thoughts?

       

    • Allan Schrum

      Our hosting services block access to sites within the pod to protect the sites from malicious behavior. That includes self-referencing request such as what you are doing. If this is critical to your business operations, you could make a request of your representative to see if this could be changed for your site.

      getAdminURL() was intended to be used by remote client rather than the server. That is why it basically doesn't work in the way you are using it.

      Regards,

      -Allan

    • Mitchell Welch

      Thanks Allan,

      That seems to be the case. I am working with an external event, so how do you access the contents of the file attachment within the external event. I have noticed that the dile attachment data is not made available in the attachment object.

      Kind regards,

      Mitch

    • Allan Schrum

      Hi Mitch,

      This is a frequently requested feature that is on our road map. Stay tuned to a future announcement.

      Regards,

      -Allan

    • Anurag Mittal

      Hi Allan,

      Any update on this "How to read/get file attachment content of an Incident from CP/CPM/Custom Script". WE have a requirement where we need to transfer Incident file attachment to SFTP server. For this we are creating a custom script that will run every day to transfer file attachment. Let me know if there is any other process to achieve it.

      Regards,

      Anurag

       

    • Bastiaan van der Kooij

      I also need this functionality. Is there no way to bypass this without having scripts on external servers?

      Bastiaan

    • Anurag Mittal

      Any one, any Idea about this?? 

    • Narendra Muttineni

      Hi Everyone,

      I also need functionality to fetch/get the file data from object. Is there anyone get idea about this how to bypass this?

      i have tried many ways but i could not succeeded. using getAdminUrl() i was able to fetch the file data and sent to third party in Test_Harness mode of the CPM. But after deploy the Custom Process code the getAdminUrl() is not working.

      Please help me to resolve this issue.

      Thanks in Advance

      Narendra Muttineni

    • Allan Schrum

      Hi Folks,

      This is a common request but we do not get complete use cases describing how this method might be used. Could someone please post their ideas to the Idea Lab so that product management will consider this need? The more details the better.

      Thanks,

      -Allan

    • Orlando Rodrigues

                      $files = $obj->FileAttachments;
                      $count = count($files);
                      self::trace($log,"File number: ".$count);
                      //if we have any attachments and if there are more than one, take only the latest
                      if($count != 0)
                      {
                          $last = $count - 1;
                          $file = $files[$last];
                          $contentType = $file->ContentType;
                          $fileName = $file->FileName;
                          self::trace($log,"Avant getAdminURL");
                          $url = $file->getAdminURL();
                          self::trace($log,"Après getAdminURL : ".$url);
                          $fileContent = self::setCurl($url);
                          $base64EncodedFile = base64_encode($fileContent);
                      }

    • Allan Schrum

      Why do you need to get the file attachment contents and base64 encode them?

      If you are trying to send an email, use the new MailMessage API to send email messages with file attachments.

      Do you have a different use case? Posting to the Idea Lab would be very good.

      Regards,

      -Allan

       

    • Orlando Rodrigues

      I'm trying to put my Attachment in an XML to send to another system.

       

    • Narendra Muttineni

      Hi Orlando,

      getAdminURL() is working in Customer Portal without any issues.

      Did you try the same in Custom Process?  I need this in custom process model. I have tried using getAdminURL() in CPM, It works great in Test_Harness mode, but same code is not working after deploy the code. 

      even i placed logs also but nothing printing after Deploy. I don't the know exact reason.

      Any small suggestion, it would be great.

      Thanks,

      Narendra Muttineni

    • Orlando Rodrigues

      Same issue, work with Test button in  process menu, not when called from RN rules.