I have been trying to create a lead from SalesForce's REST API for the past several days but I can't for the life of me get it working. I am able to get the access token no problem but from there on as far as creating a lead I am having absolutely no luck at all.
I keep seeing in all of the documentation this:
curl https://na1.salesforce.com/services/data/v20.0/sobjects/Account/ -H "Authorization: Bearer token -H "Content-Type: application/json" -d #newaccount.json"
How would I do this in PHP's curl though? I have tried and tried but had no luck at all.
Here is how I have got the access token:
$ch = curl_init();
// set URL options
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, "https://login.salesforce.com/services/oauth2/token?grant_type=password&client_id=".CONSUMER_KEY."&client_secret=".CONSUMER_SECRET."&username=".USERNAME."&password=".USERPASS.SECURITY_TOKEN);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// grab HTML
$data = curl_exec($ch);
$data = json_decode($data, true);
$code = $data['access_token'];
curl_close($ch);
I have tried doing something like this after this code, however I have had no luck.
$token_url = LOGIN_BASE_URL.'/services/oauth2/token';
$post_fields = array(
'code' => $code,
'grant_type' => 'authorization_code',
'client_id' => CONSUMER_KEY,
'client_secret' => CONSUMER_SECRET,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $token_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$token_request_body = curl_exec($ch)
I just need to figure out how to create leads in SalesForce, I have no idea where to go from here. Any help would be greatly appreciated as I cannot find decent documentation anywhere that helps me.
create account demo, should get you started:
function create_account($name, $instance_url, $access_token) {
$url = "$instance_url/services/data/v20.0/sobjects/Account/";
$content = json_encode(array("Name" => $name));
$curl = curl_init($url);
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, $content);
$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 creating account<br/><br/>";
curl_close($curl);
$response = json_decode($json_response, true);
$id = $response["id"];
echo "New record id $id<br/><br/>";
return $id;
}
Related
I am trying to get data from a JSON endpoint on my streaming server. I've read that i need to pass an API key through the X-API-Key header. But not sure how to do this.
$url = file_get_contents('XXXXX/history');
$data1 = json_decode($url,true);
var_dump($data1);
this code works for me when getting json data from files that dont require the api key. How ever this now returns NULL as the key isn't being submitted through.
Any ideas?
try this:
$url = 'XXXXX/history';
$options = array('http' => array(
'method' => 'GET',
'header' => 'Authorization: Bearer '.$yourApiKey
));
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
try with CURL:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, 'XXXXX/history');
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3000); // 3 sec.
curl_setopt($ch, CURLOPT_TIMEOUT, 10000); // 10 sec.
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: ' . $yourApiKey
));
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
I m try to update some data on mongodb using cms api
on terminal i can update information like this
curl -X PUT -d name=12345a https://api1.MYWEBISITE.com/api/v1in/user/2039/?t=mytoken
now using PHP i have try so many ways and no one looks to work for me
tried like this
class Curl {
public function put($url, $data_string){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
return $result;
}
}
$curl = new Curl;
$data_string = '{"name" : "name123"}';
$url = "https://api1.MYWEBSITE.com/api/v1in/user/2039/?t=mytoken";
echo $curl->put($url, $data_string);
also i tried like this
$data = array( "name" => '12344');
$url = "https://api1.mywebsite.com/api/v1in/user/2039/?t=mytoken";
$curl = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
$response = json_decode($result);
var_dump($response);
curl_close($curl);
both php solutions dont works, nothing is updated, no error msg is showing
any help?
I am in the process of creating a super simple PHP page that sends JSON data to an remote API via POST. I am having issues with it and hoping someone here could help me out.
This is what I have so far
$url = "https://remoteHost.com/api/post";
$json = '{"message":"textHere", "user":"userABC"}';
$ch = curl_init();
$timeout = 0;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Basic --ACB123-ABC123---ABC123---',
'Content-Type: application/json',
'Content-Length: ' . strlen($json),)
);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
When I execute this nothing happens. The page loads without errors but it looks like the curl isn't actually doing anything. Any Ideas?
I want to add tracks to my PRIVATE Spotify playlist by php - but i don´t get it done. I´m always getting the error message
Error Code 403 - This request requires user authentication.
I'm not the good in the spotify api. Can someone help me out? This is my code:
<?php
error_reporting(E_ALL);
$url = 'https://accounts.spotify.com/api/token';
$method = 'POST';
$credentials = "Client ID:Client Secret";
$headers = array(
"Accept: */*",
"Content-Type: application/x-www-form-urlencoded",
"Authorization: Basic " . base64_encode($credentials));
$data = 'grant_type=client_credentials';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response_1 = curl_exec($ch);
curl_close($ch);
$response_1 = json_decode($response_1, true);
var_dump($response_1);
$token = $response_1['access_token'];
echo $token."<br>";
$headers_2 = array(
"Accept: application/json",
"Content-Type: application/x-www-form-urlencoded",
('Authorization: Bearer ' . $token));
$url_2 = 'https://api.spotify.com/v1/users/example/playlists/playlist_example_id/tracks';
$postargs = '?uris=spotify%3Atrack%3A4xwB7i0OMsmYlQGGbtJllv';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_2);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_2);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postargs);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response_2 = curl_exec($ch);
curl_close($ch);
$response_2 = json_decode($response_2, true);
var_dump($response_2);
I've looked around a bit and can't find a solution that solves my problem. I'm getting the following error: "The requested URL returned error: 413."
I'm posting some information via curl and PHP to a URL via HTTPS. I'm told that I have to pass the "Content-Length" along with my request since the destination is https and not http.
Here's the code I'm using:
$user = '11111111111111111';
$books = array(111);
$data = array("action" => "books-shared", "userID" => $user, "bookIDs" => $books);
$data_string = array('json'=>json_encode($data));
$target_url = 'https://www.test.com/test.php'; // fake URL of course
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$api = curl_exec($ch);
My strlen($data_string) command is returning a value of 5, which is much less than the actual length of the $data_string, which is much longer. I assume this is the problem unless someone thinks it might be being caused by something else.
Here's what I ended up with:
$user = '11111111111111111';
$books = array(111);
$data = array("action" => "books-shared", "userID" => $user, "bookIDs" => $books);
$data_string = array('json'=>json_encode($data));
$target_url = 'https://www.test.com/test.php'; // fake URL of course
$ch = curl_init();
if (!$ch) {
die("Couldn't initialize a cURL handle");
}
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
$api = curl_exec($ch);
No SSL or size errors and it seems to work fine.