Been searching for awhile but haven't been able to find any examples.. I'm trying to upload a remote PDF file to an Account record in SF using PHP cURL / Json, is that possible and if so do you have a basic example showing how?
EDIT -- here's the code I'm using:
define("USERNAME", "[email protected]");
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);
Which returns the following 201 Response:
array(3) { ["id"]=> string(18) "00P5B000000NOU4UAO" ["success"]=> bool(true) ["errors"]=> array(0) { } }
However, I'm not seeing the File under the Account or anywhere in Salesforce