Advertisement
Guest User

Untitled

a guest
Jul 21st, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. <?php
  2.  
  3. // You need to set these three to the values for your own application
  4. define('CONSUMER_KEY', '');
  5. define('CONSUMER_SECRET', '');
  6. define('REDIRECT_URI', '');
  7. define('LOGIN_BASE_URL', '');
  8. $token_url = LOGIN_BASE_URL.'';
  9. $username = '';
  10. $password = '';
  11.  
  12.  
  13. // This example uses PHP sessions to save the authorization tokens. If you plan on
  14. // deploying across multiple machines behind a load-balancer, be aware you'll need
  15. // to use something more sophisticated
  16. session_start();
  17.  
  18. $post_fields = array(
  19. 'code' => $code,
  20. 'grant_type' => 'password',
  21. 'client_id' => CONSUMER_KEY,
  22. 'client_secret' => CONSUMER_SECRET,
  23. 'redirect_uri' => REDIRECT_URI,
  24. 'username' => $username,
  25. 'password' => $password,
  26. );
  27.  
  28. $ch = curl_init();
  29. curl_setopt($ch, CURLOPT_URL, $token_url);
  30. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  31. // curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
  32. curl_setopt($ch, CURLOPT_POST, TRUE);
  33. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
  34.  
  35. // Make the API call, and then extract the information from the response
  36. $token_request_body = curl_exec($ch)
  37. or die("Call to get token from code failed.");
  38.  
  39. $token_response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  40. if (($token_response_code<200)||($token_response_code>=300)||empty($token_request_body))
  41. die("Call to get token from code failed.'");
  42.  
  43. $token_request_data = json_decode($token_request_body, true);
  44. if (empty($token_request_data))
  45. die("Couldn't decode '$token_request_data' as a JSON object");
  46.  
  47. if (!isset($token_request_data['access_token'])||
  48. !isset($token_request_data['instance_url']))
  49. die("Missing expected data.");
  50.  
  51. // Save off the values we need for future use
  52. $_SESSION['access_token'] = $token_request_data['access_token'];
  53. $_SESSION['instance_url'] = $token_request_data['instance_url'];
  54.  
  55. // Redirect to the main page without the code in the URL
  56. ///////////////////// header('Location: '.REDIRECT_URI); /////////////////////
  57.  
  58. // If we're here, we must have a valid session containing the access token for the
  59. // API, so grab it ready for subsequent use
  60. $access_token = $_SESSION['access_token'];
  61. $instance_url = $_SESSION['instance_url'];
  62.  
  63. error_log("access_token: '$access_token'");
  64.  
  65. //CREATE DATA
  66. function create_lead(
  67. $SF_Contact_ID,
  68. $SF_Passport_Given_Name,
  69. $SF_Passport_Middle_Name,
  70. $SF_Passport_Surname,
  71. $SF_Passport_Number,
  72. $SF_Preferred_Airline,
  73. $SF_Frequent_Flyer_Program_Carriers,
  74. $instance_url,
  75. $access_token) {
  76. $url = "$instance_url/services/data/v20.0/sobjects/Passport_and_Frequent_Flyer__c/";
  77.  
  78. //MAP SALESFORCE FIELDS
  79. $content = json_encode(array(
  80. "Contact__c" => $SF_Contact_ID,
  81. "Passport_Given_Name__c" => $SF_Passport_Given_Name,
  82. "Passport_Middle_Name__c" => $SF_Passport_Middle_Name,
  83. "Passport_Surname__c" => $SF_Passport_Surname,
  84. "Passport_Number__c" => $SF_Passport_Number,
  85. "Preferred_Airline__c" => $SF_Preferred_Airline,
  86. "Frequent_Flyer_Program_Carriers_1__c" => $SF_Frequent_Flyer_Program_Carriers
  87. ));
  88.  
  89. $curl = curl_init($url);
  90. curl_setopt($curl, CURLOPT_HEADER, false);
  91. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  92. curl_setopt($curl, CURLOPT_HTTPHEADER,
  93. array("Authorization: OAuth $access_token",
  94. "Content-type: application/json"));
  95. curl_setopt($curl, CURLOPT_POST, true);
  96. curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
  97.  
  98. $json_response = curl_exec($curl);
  99.  
  100. $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  101.  
  102. curl_close($curl);
  103. }
  104. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement