Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function sfWrap($userEmail){
- define("USERNAME", "myuser@mydomain.com.sandbox");
- define("PASSWORD", "MyPasswordandSecurityToken");
- define('CLIENT_ID', 'MYCLIENTID');
- define('CLIENT_SECRET', 'MYCLIENTSECRET');
- $loginurl = "https://test.salesforce.com/services/oauth2/token";
- $params = "grant_type=password"
- . "&client_id=" . CLIENT_ID
- . "&client_secret=" . CLIENT_SECRET
- . "&username=" . USERNAME
- . "&password=" . PASSWORD;
- $curl = curl_init($loginurl);
- curl_setopt($curl, CURLOPT_HEADER, false);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_POST, true);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
- $json_response = curl_exec($curl);
- $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
- if ( $status != 200 ) {
- die("Error: call to URL failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
- }
- curl_close($curl);
- $sfData = json_decode($json_response);
- $access_token = $sfData->access_token;
- $instance_url = $sfData->instance_url;
- $url = $instance_url . "/services/data/v20.0/sobjects/Attachment/";
- $accId= '0015B000002MzlbQAC';
- $fp = fopen('MyFile.pdf', 'r');
- $content = fread($fp, filesize('MyFile.pdf'));
- $fileData = base64_encode($content);
- $data = array("ParentId" => $accId, "Name" => 'MyFile.pdf', "body" => "$fileData", "IsPrivate" => "false");
- $data_string = json_encode($data);
- $curl = curl_init($url);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_HEADER, false);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_HTTPHEADER,
- array("Authorization: OAuth $access_token","Content-type: application/json"));
- curl_setopt($curl, CURLOPT_POST, true);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
- $json_response = curl_exec($curl);
- $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
- if ( $status != 201 ) {
- die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
- }
- // echo "HTTP status $status uploading file failed<br/><br/>";
- curl_close($curl);
- $response = json_decode($json_response, true);
- var_dump($response);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement