You're very close - the tricky bit is that the Chatter REST API is expecting a multipart form. Also, it wants the message segments as its own array. After some poking around it looks like the easiest way to handle it is by wrangling the post data together.
I'm using this doc as a base: http://www.salesforce.com/us/developer/docs/chatterapi/Content/intro_input.htm
And here is my script, note that I pasted the url manually (which you should not):
<?php
$url = "https://na15.salesforce.com/services/data/v29.0/chatter/feeds/record/500i0000004AcIq/feed-items";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
$fileName = basename($file);
$headers = array();
$headers[] = "Authorization: OAuth {token}";
$headers[] = 'Content-Type: multipart/form-data; boundary=F9jBDELnfBLAVmLNbnLIYibT5Icp0h3VJ7mkI';
$segments = array();
$segments['type'] = 'Text';
$segments['text'] = 'User uploaded attachment';
$message = array();
$message[] = $segments;
$dataArray = array(
'body' => array(
'messageSegments' => $message
),
'attachment' => array(
'attachmentType' => 'NewFile',
'title' => 'Test File'
)
);
$data = $data.json_encode($dataArray);
$file = realpath('phoenix-logo.png');
$post_text = '--F9jBDELnfBLAVmLNbnLIYibT5Icp0h3VJ7mkI
Content-Disposition: form-data; name="json"
Content-Type: application/json; charset=UTF-8
{ "body":
{
"messageSegments" : [
{
"type" : "Text",
"text" : "Here is another file for review."
}, {
"type" : "Hashtag",
"tag" : "important"
}, {
"type" : "Text",
"text" : "Again, please review this as soon as possible."
}
]
},
"attachment":
{
"attachmentType" : "NewFile",
"description": "Phoenix Logo",
"title" : "Logo.png"
}
}
--F9jBDELnfBLAVmLNbnLIYibT5Icp0h3VJ7mkI
Content-Disposition: form-data; name="feedItemFileUpload"; filename="logo.png"
Content-Type: image/png
'.file_get_contents($file).'
--F9jBDELnfBLAVmLNbnLIYibT5Icp0h3VJ7mkI--';
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_text);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
$response_json = curl_exec($curl);
print $response_json;
?>
For others having the same issue you could be missing the security token:
For access via the API or client app, if the Multi-Factor
Authentication on API Logins permission is set on the user profile,
users enter a TOTP verification code generated by an authenticator
app. If the permission isn’t set, users must add their security token
to the end of their password to log in. A security token is a
generated key from Salesforce. For example, if a user’s password is
mypassword and the security token is XXXXXXXXXX, the user enters
mypasswordXXXXXXXXXX to log in. Some client apps have a separate field
for the security token.
Users can get their security token by changing their password or
resetting their security token via the Salesforce user interface. When
a user changes a password or resets a security token, Salesforce sends
a new security token to the email address on the user’s Salesforce
record. The security token is valid until the user resets the security
token, changes a password, or has a password reset.
Reference: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_concepts_security.htm
Best Answer
test.salesforce.com
orlogin.salesforce.com
can be used to first login. Thereafter requests should be made using theinstance_url
and theaccess_token
that you receive as a response on first login.So, in your case you can used your Custom Domain but not the login URL.