Guest User

Untitled

a guest
Nov 2nd, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.62 KB | None | 0 0
  1. curl_setopt($curl, CURLOPT_URL, "https://" . $this->salesforce_host . "/services/oauth2/token");
  2. curl_setopt($curl, CURLOPT_POSTFIELDS, "client_id=" . $this->salesforce_client_id. "&grant_type=password&client_secret=" . $this->salesforce_client_secret . "&username=" . $this->salesforce_username . "&password=" . $this->salesforce_password . $this->salesforce_token);
  3. curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded;"));
  4.  
  5. // https://$salesforceurl/services/data/v29.0/chatter/feeds/record/$caseId/feed-items
  6. $session = $this->getSession();
  7. $url = $session["instanceUrl"] . "/services/data/v" . $this->salesforce_version . "/chatter/feeds/record/" . $parentId . '/feed-items';
  8.  
  9. $curl = $this->_getDefaultCurl();
  10. curl_setopt($curl, CURLOPT_URL, $url);
  11. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
  12.  
  13. //get file mime type and binary content of file
  14.  
  15. // form field separator
  16. $delimiter = uniqid();
  17. $fileFields = array(
  18. 'fileFeedItem' => array(
  19. 'type' => $finfo_mime,
  20. 'content' => implode('', $buffer)
  21. ),
  22. );
  23.  
  24. $data = '';
  25.  
  26. $data .= "--" . $delimiter . "rn";
  27. $data .= 'Content-Disposition: form-data; name="json";' . "rn";
  28. $data .= 'Content-Type: application/json; charset=UTF-8;' . "rnrn";
  29. $fileName = basename($file);
  30. $json = array(
  31. 'body' => array(
  32. 'messageSegments' => array(
  33. 'type' => 'Text',
  34. 'text' => 'User uploaded attachment'
  35. )
  36. ),
  37. 'attachment' => array(
  38. 'attachmentType' => 'NewFile',
  39. 'title' => $fileName
  40. ),
  41.  
  42. );
  43. $data .= json_encode($json)."rn";
  44. $data .= "rnrn";
  45.  
  46.  
  47. foreach ($fileFields as $name => $fileItem) {
  48. $data .= "--" . $delimiter . "rn";
  49. $data .= 'Content-Disposition: form-data; name="feedItemFileUpload"; filename="' .$fileName . '"' . "rn";
  50. $data .= 'Content-Type: ' . $fileItem['type'] . "rn";
  51. $data .= 'Content-Transfer-Encoding: binary' . "rn";
  52. $data .= "rn";
  53. $data .= $fileItem['content'] . "rn";
  54. }
  55. $data .= "--" . $delimiter . "--rn";
  56.  
  57. $headers = '';
  58. $headers .= "Accept: application/xml"."rn";
  59. $headers .= "Content-Type: multipart/form-data; boundary=".$delimiter."rn";
  60. $headers .= "Authorization: OAuth ". $session["sessionId"] ."rn";
  61. $headers .= "Host: ". $session["instanceUrl"] ."rn";
  62. $headers .= "Content-Length: " . strlen($data) . "rn";
  63. $headers .= "Expect: 100-continue" . "rn";
  64. $headers .= "rn";
  65.  
  66. $data = $headers . $data;
  67.  
  68. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  69.  
  70. $response_json = curl_exec($curl);
  71.  
  72. Accept: application/xml
  73. Content-Type: multipart/form-data; boundary=52dd7f7e5fd87
  74. Authorization: OAuth <session_id>
  75. Host: https://cs30.salesforce.com
  76. Content-Length: 494
  77. Expect: 100-continue
  78.  
  79. --52dd7f7e5fd87
  80. Content-Disposition: form-data; name="json";
  81. Content-Type: application/json; charset=UTF-8;
  82.  
  83. {"body":{"messageSegments":{"type":"Text","text":"User uploaded attachment"}},"attachment":{"attachmentType":"NewFile","title":"attachment_test.txt"}}
  84.  
  85.  
  86. --52dd7f7e5fd87
  87. Content-Disposition: form-data; name="feedItemFileUpload"; filename="attachment_test.txt"
  88. Content-Type: text/plain; charset=us-ascii
  89. Content-Transfer-Encoding: binary
  90.  
  91. �ttachment_test
  92. --52dd7f7e5fd87--
  93.  
  94. $session = $this->myLogin();
  95. $url = $session["instanceUrl"] . "/services/data/v" . $this->salesforce_version . "/chatter/feeds/record/" . $parentId . '/feed-items';
  96.  
  97. $curl = $this->_getDefaultCurl();
  98. curl_setopt($curl, CURLOPT_URL, $url);
  99. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
  100.  
  101. $fileName = basename($file);
  102. $json = array(
  103. 'body' => array(
  104. 'messageSegments' => array(
  105. 'type' => 'Text',
  106. 'text' => 'User uploaded attachment'
  107. )
  108. ),
  109. 'attachment' => array(
  110. 'attachmentType' => 'NewFile',
  111. 'title' => $fileName
  112. ),
  113. );
  114.  
  115. $headers = array();
  116. $headers[] = "Authorization: OAuth ". $session["sessionId"];
  117. $headers[] = "Content-Type: application/json; charset=utf-8";
  118. $headers[] = "Accept: application/json";
  119.  
  120. $fileArray = array(
  121. 'json' =>$json,
  122. 'feedItemFileUpload' => '@'.realpath($file),
  123. );
  124.  
  125. $data = json_encode($fileArray);
  126.  
  127. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  128. curl_setopt($curl, CURLOPT_POST, true);
  129. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  130. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  131. curl_setopt($curl, CURLINFO_HEADER_OUT, true);
  132.  
  133. $response_json = curl_exec($curl);
  134.  
  135. <?php
  136. $url = "https://na15.salesforce.com/services/data/v29.0/chatter/feeds/record/500i0000004AcIq/feed-items";
  137. $curl = curl_init();
  138. curl_setopt($curl, CURLOPT_URL, $url);
  139. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
  140.  
  141. $fileName = basename($file);
  142.  
  143. $headers = array();
  144. $headers[] = "Authorization: OAuth {token}";
  145. $headers[] = 'Content-Type: multipart/form-data; boundary=F9jBDELnfBLAVmLNbnLIYibT5Icp0h3VJ7mkI';
  146.  
  147. $segments = array();
  148. $segments['type'] = 'Text';
  149. $segments['text'] = 'User uploaded attachment';
  150.  
  151. $message = array();
  152. $message[] = $segments;
  153.  
  154. $dataArray = array(
  155. 'body' => array(
  156. 'messageSegments' => $message
  157. ),
  158. 'attachment' => array(
  159. 'attachmentType' => 'NewFile',
  160. 'title' => 'Test File'
  161. )
  162. );
  163.  
  164. $data = $data.json_encode($dataArray);
  165. $file = realpath('phoenix-logo.png');
  166.  
  167.  
  168. $post_text = '--F9jBDELnfBLAVmLNbnLIYibT5Icp0h3VJ7mkI
  169. Content-Disposition: form-data; name="json"
  170. Content-Type: application/json; charset=UTF-8
  171.  
  172. { "body":
  173. {
  174. "messageSegments" : [
  175. {
  176. "type" : "Text",
  177. "text" : "Here is another file for review."
  178. }, {
  179. "type" : "Hashtag",
  180. "tag" : "important"
  181. }, {
  182. "type" : "Text",
  183. "text" : "Again, please review this as soon as possible."
  184. }
  185. ]
  186. },
  187. "attachment":
  188. {
  189. "attachmentType" : "NewFile",
  190. "description": "Phoenix Logo",
  191. "title" : "Logo.png"
  192. }
  193. }
  194.  
  195. --F9jBDELnfBLAVmLNbnLIYibT5Icp0h3VJ7mkI
  196. Content-Disposition: form-data; name="feedItemFileUpload"; filename="logo.png"
  197. Content-Type: image/png
  198.  
  199. '.file_get_contents($file).'
  200.  
  201. --F9jBDELnfBLAVmLNbnLIYibT5Icp0h3VJ7mkI--';
  202.  
  203. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  204. curl_setopt($curl, CURLOPT_POST, true);
  205. curl_setopt($curl, CURLOPT_VERBOSE, 1);
  206. curl_setopt($curl, CURLOPT_POSTFIELDS, $post_text);
  207. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  208. curl_setopt($curl, CURLINFO_HEADER_OUT, true);
  209.  
  210. $response_json = curl_exec($curl);
  211.  
  212. print $response_json;
  213. ?>
Add Comment
Please, Sign In to add comment