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 
    2300 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

    • rbrekke

      Narendra and Orlando, did either of you manage to figure this out? I am trying to do the same, in fact exactly the same as you Orlando. I am not having much luck in the test harness however, I can get the getAdminURL() but not much luck in the encode. Orlando it looks like you are using curl to get the content, can you explain that a bit, maybe include that code.

      I was having problems with cURL in deploy mode(my CPM is an XML request to upload incident info from Oracale RightNow to external incident management system) without attachments, later I found that the RightNow utility server needed to be whitelisted, which maybe your problem if using cURL (maybe you figured that out!). Anyway I was hoping to at least get the attachment piece working in the test harness if possible, but having a bit of a roadblock getting the content back without file_get_contents.

      Any help would be appreciated,

      Thanks

      Rob

    • Orlando Rodrigues

       

      Hi,

       

      I had to call my own OSC web service to get the file encoded, put it in a variable in order to send it to the other system.

      Pretty ugly, but working...

       

      O.

       

    • Narendra Muttineni

      Hi,

      You can bypass this by using getFileAttachment() soap Function. As Oralndo said it is pretty ugly but it is working successfully. 

      getFileAttachment() is the RightNow soap function, so you can get the file of an object using this function.

       $fileattachments = $incident->FileAttachments;
        for($i=0; $i<count($fileattachments); $i++)
        {
             $fname = $fileattachments[$i]->FileName;
              if(strpos($fname, 'Backlog incidents') !== false)
              {
                     $fid = $fileattachments[$i]->ID;
                      break;
               }
          }
      
      <ns7:GetFileData xmlns:ns7="urn:messages.ws.rightnow.com/v1_2">
      <ns7:RNObject xmlns:ns4="urn:objects.ws.rightnow.com/v1_2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns4:Incident">
       <ID xmlns="urn:base.ws.rightnow.com/v1_2" id="'.$id.'" /> // ID of an Incident.
       </ns7:RNObject>
        <ns7:FileID id="'.$fid.'"/> // File Attachment ID of an Incident.
         <ns7:DisableMTOM>true</ns7:DisableMTOM>
          </ns7:GetFileData>
      

      You could execute this soap function using CURL functionality. 

      Hope it could work for you.

      Regards,

      Narendra

    • rbrekke

      Thanks Narendra and Orlando. I will definately give this a try

      Rob

       

    • Vikas Hanagodimath
      Narendra Muttineni said:

      Hi,

      You can bypass this by using getFileAttachment() soap Function. As Oralndo said it is pretty ugly but it is working successfully. 

      getFileAttachment() is the RightNow soap function, so you can get the file of an object using this function.

       $fileattachments = $incident->FileAttachments;
        for($i=0; $i<count($fileattachments); $i++)
        {
             $fname = $fileattachments[$i]->FileName;
              if(strpos($fname, 'Backlog incidents') !== false)
              {
                     $fid = $fileattachments[$i]->ID;
                      break;
               }
          }
      
      <ns7:GetFileData xmlns:ns7="urn:messages.ws.rightnow.com/v1_2">
      <ns7:RNObject xmlns:ns4="urn:objects.ws.rightnow.com/v1_2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns4:Incident">
       <ID xmlns="urn:base.ws.rightnow.com/v1_2" id="'.$id.'" /> // ID of an Incident.
       </ns7:RNObject>
        <ns7:FileID id="'.$fid.'"/> // File Attachment ID of an Incident.
         <ns7:DisableMTOM>true</ns7:DisableMTOM>
          </ns7:GetFileData>
      

      You could execute this soap function using CURL functionality. 

      Hope it could work for you.

      Regards,

      Narendra

      View original

      Hi Narendra, can you provide a sample code for fetching the incident attachment please i.e executing SOAP function using cURL functionality.

    • Narendra Muttineni

      Hi,

      Here is the SOAP request to fetch the file data from Incident.

      $fileattachments = $incident->FileAttachments;
        for($i=0; $i<count($fileattachments); $i++)
        {
             $fname = $fileattachments[$i]->FileName;
              if(strpos($fname, 'Backlog incidents') !== false)
              {
                     $fid = $fileattachments[$i]->ID;
                      break;
               }
          }
      
      $body = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1_2="urn:messages.ws.rightnow.com/v1_2" xmlns:v1_2="urn:base.ws.rightnow.com/v1_2">
                      <soapenv:Header>
                          <ns7:ClientInfoHeader xmlns:ns7="urn:messages.ws.rightnow.com/v1_2" soapenv:mustUnderstand="0">
                              <ns7:AppID>Basic Get File Data</ns7:AppID>
                          </ns7:ClientInfoHeader>
                          <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" mustUnderstand="1">
                              <wsse:UsernameToken>
                                  <wsse:Username>uname</wsse:Username>
                                  <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">pwd</wsse:Password>
                              </wsse:UsernameToken>
                          </wsse:Security>
                      </soapenv:Header>
                      <soapenv:Body>
                          <ns7:GetFileData xmlns:ns7="urn:messages.ws.rightnow.com/v1_2">
                   <ns7:RNObject xmlns:ns4="urn:objects.ws.rightnow.com/v1_2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns4:Incident">
                             <ID xmlns="urn:base.ws.rightnow.com/v1_2" id="<Incident ID>" />
                   </ns7:RNObject>
                  <ns7:FileID id="<File attachment ID>"/>
                  <ns7:DisableMTOM>true</ns7:DisableMTOM>
                          </ns7:GetFileData>
                      </soapenv:Body>
                  </soapenv:Envelope>'
      

      Regards,

      Narendra 

    • Vikas Hanagodimath

      Hi Narendra,

       

      Thank you for quick reply, i have one more question. What should be the URL ?

      Is this correct https://XYZ--tst1.custhelp.com/cgi-bin/XYZ.cfg/services/soap?wsdl

      Sorry i am new to OSC.

    • Mark Rhoads

      Using the OSvC service as the integration platform is not recommended.

      PHP code in OSvC is usually time boxed, so you have no guarantee that you'll be able to finish processing that CSV file.

      And, making that SOAP call back into the OSvC service is not supported and is not guaranteed to always work -- even if you do find a way to make it work.

      The best way to accomplish this is to perform the CSV processing on another platform, and integrate to OSvC with Connect Web Services for SOAP or Connect REST API's .

       

    • Mark Rhoads

      Vikas, what exactly is in this CSV file and what exactly is being done as a result of the contents?

    • Jaya Lakshmi

      Hi All,

      I'm trying to call the SOAP function from CPM but I'm receiving time out error .Could any one please help me to sort out this.


                          $body = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1_2="urn:messages.ws.rightnow.com/v1_2" xmlns:v1_2="urn:base.ws.rightnow.com/v1_2">
                                     <soapenv:Body>
                          <ns7:GetFileData xmlns:ns7="urn:messages.ws.rightnow.com/v1_2">
                   <ns7:RNObject xmlns:ns4="urn:objects.ws.rightnow.com/v1_2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns4:Incident">
                             <ID xmlns="urn:base.ws.rightnow.com/v1_2" id="'.$id.'" />
                   </ns7:RNObject>
                  <ns7:FileID id="'.$fid.'"/>
                  <ns7:DisableMTOM>true</ns7:DisableMTOM>
                          </ns7:GetFileData>
                      </soapenv:Body>
                  </soapenv:Envelope>';
                  echo $body;
                   $headers = array(
                              "Content-type: text/xml;charset=\"utf-8\"",
                              "Accept: text/xml",
                              "Cache-Control: no-cache",
                              "Pragma: no-cache",
                              //"SOAPAction: activity/create_activity", 
                              "Content-length: 8000",
                          ); //SOAPAction: your op URL

      print_r($headers);
                  // Loads curl library functions if exists    
      if (!function_exists("\curl_init"))
      {
                      \load_curl();
      }

      $ch = curl_init();    
      $usrpwd= $soapUser.':'.$soapPassword;
      $ch = \curl_init();
      $url="https://test.custhelp.com/cgi-bin/test.cfg/services/soap?wsdl";
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_USERPWD, $usrpwd);
      curl_setopt($ch, CURLOPT_VERBOSE, 1);
      curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_ANY);
      curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, true);
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $body); // the SOAP request
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0); 
      //curl_setopt($ch, CURLOPT_TIMEOUT, 400);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
      $content=curl_exec($ch);
      $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
      echo $code;

        

    • Narendra Muttineni

      Hi Jayalakshmi,

      The URL should not be the WSDL URL. The URL should be SOAP URL $url="https://test.custhelp.com/cgi-bin/test.cfg/services/soap";

      Regards,
      Narendra

    • Dietrik

      Why is this old post being updated? Please start your own.

    • Premkumar Sabarinathan

      Jayalakshmi,

      Please start a new post.And  Why you are using Connect Webservices in CPM instead of Connect PHP API ??

      Thanks

      Prem