Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $data = array('name' => 'Mani Test', 'phone' => '9876545673', 'email' => 'manitest234@gmail.com');
- fetchToken($data);
- function fetchToken($data){
- $url = 'https://my335271.crm.ondemand.com/sap/c4c/odata/cust/v1/zserviceticket/zknserviceticketCollection';
- $user = 'PDI_USER';
- $pass = 'Welcome@12345';
- $result = sendRequest($url, array(), $user, $pass);
- $token = $result;
- echo $token;
- $_result = sendCRM($data, $token);
- }
- function sendCRM($data, $token){
- $url = 'https://my335271.crm.ondemand.com/sap/c4c/odata/cust/v1/zserviceticket/zknserviceticketCollection';
- /* parsing crm data */
- $_data = array();
- $_data['CustomerFName'] = rawurlencode($data['name']);
- $_data['CustomerLName'] = rawurlencode('');
- $_data['CustomerPhone'] = rawurlencode($data['phone']);
- $_data['CustomerEmail'] = rawurlencode($data['email']);
- $_data['DetailsOfSource'] = rawurlencode('website');
- $_data['SalesOrgLead'] = 'B200';
- $user = 'USER';
- $pass = 'PASS';
- $result = sendRequest($url, $_data, $user, $pass, $token);
- echo 'Result : '.$result;
- }
- function sendRequest($url, $data, $user='', $pass = '', $token = false){
- $params = '';
- foreach($data as $key=>$value)
- $params .= $key.'='.$value.'&';
- $params = trim($params, '&');
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url); //Remote Location URL
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Return data instead printing directly in Browser
- // curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 7); //Timeout after 7 seconds
- // curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
- curl_setopt($ch, CURLOPT_HEADER, 1);
- if(!$token)
- curl_setopt($ch, CURLOPT_HTTPHEADER, ['X-CSRF-Token:Fetch']);
- else
- curl_setopt($ch, CURLOPT_HTTPHEADER, [$token]);
- curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
- //We add these 2 lines to create POST request
- if($token){
- curl_setopt($ch, CURLOPT_POST, count($data)); //number of parameters sent
- curl_setopt($ch, CURLOPT_POSTFIELDS, $params); //parameters data
- }
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- $result = curl_exec($ch);
- if(curl_errno($ch)) //catch if curl error exists and show it
- return array('error' => curl_error($ch));
- else{
- curl_close($ch);
- if(!$token){
- $result = split("\n", $result);
- $csrf = trim($result[6]);
- return str_replace(' ', '', $csrf);
- }
- else
- return $result;
- }
- }
- ?>
Add Comment
Please, Sign In to add comment