Guest User

C4C

a guest
Dec 15th, 2017
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. <?php
  2.  
  3. $data = array('name' => 'Mani Test', 'phone' => '9876545673', 'email' => 'manitest234@gmail.com');
  4.  
  5. fetchToken($data);
  6.  
  7. function fetchToken($data){
  8. $url = 'https://my335271.crm.ondemand.com/sap/c4c/odata/cust/v1/zserviceticket/zknserviceticketCollection';
  9.  
  10. $user = 'PDI_USER';
  11. $pass = 'Welcome@12345';
  12.  
  13. $result = sendRequest($url, array(), $user, $pass);
  14. $token = $result;
  15. echo $token;
  16. $_result = sendCRM($data, $token);
  17.  
  18. }
  19.  
  20. function sendCRM($data, $token){
  21. $url = 'https://my335271.crm.ondemand.com/sap/c4c/odata/cust/v1/zserviceticket/zknserviceticketCollection';
  22.  
  23. /* parsing crm data */
  24. $_data = array();
  25. $_data['CustomerFName'] = rawurlencode($data['name']);
  26. $_data['CustomerLName'] = rawurlencode('');
  27. $_data['CustomerPhone'] = rawurlencode($data['phone']);
  28. $_data['CustomerEmail'] = rawurlencode($data['email']);
  29. $_data['DetailsOfSource'] = rawurlencode('website');
  30. $_data['SalesOrgLead'] = 'B200';
  31.  
  32. $user = 'USER';
  33. $pass = 'PASS';
  34.  
  35. $result = sendRequest($url, $_data, $user, $pass, $token);
  36. echo 'Result : '.$result;
  37.  
  38.  
  39. }
  40.  
  41. function sendRequest($url, $data, $user='', $pass = '', $token = false){
  42. $params = '';
  43. foreach($data as $key=>$value)
  44. $params .= $key.'='.$value.'&';
  45.  
  46. $params = trim($params, '&');
  47.  
  48. $ch = curl_init();
  49.  
  50. curl_setopt($ch, CURLOPT_URL, $url); //Remote Location URL
  51. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Return data instead printing directly in Browser
  52. // curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 7); //Timeout after 7 seconds
  53. // curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
  54. curl_setopt($ch, CURLOPT_HEADER, 1);
  55. if(!$token)
  56. curl_setopt($ch, CURLOPT_HTTPHEADER, ['X-CSRF-Token:Fetch']);
  57. else
  58. curl_setopt($ch, CURLOPT_HTTPHEADER, [$token]);
  59.  
  60. curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
  61.  
  62. //We add these 2 lines to create POST request
  63. if($token){
  64. curl_setopt($ch, CURLOPT_POST, count($data)); //number of parameters sent
  65. curl_setopt($ch, CURLOPT_POSTFIELDS, $params); //parameters data
  66. }
  67. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  68.  
  69.  
  70. $result = curl_exec($ch);
  71.  
  72. if(curl_errno($ch)) //catch if curl error exists and show it
  73. return array('error' => curl_error($ch));
  74. else{
  75. curl_close($ch);
  76. if(!$token){
  77. $result = split("\n", $result);
  78. $csrf = trim($result[6]);
  79. return str_replace(' ', '', $csrf);
  80. }
  81. else
  82. return $result;
  83. }
  84. }
  85.  
  86.  
  87. ?>
Add Comment
Please, Sign In to add comment