php - 在 PHP 中使用 REST API 在 SalesForce 中创建销售线索

标签 php api rest curl salesforce

在过去的几天里,我一直在尝试通过 SalesForce 的 REST API 创建销售线索,但我终究无法让它发挥作用。我能够毫无问题地获得访问 token ,但从那时起,就创建潜在客户而言,我完全没有运气。

我一直在所有文档中看到:

curl https://na1.salesforce.com/services/data/v20.0/sobjects/Account/ -H "Authorization: Bearer token -H "Content-Type: application/json" -d @newaccount.json"

不过,我如何在 PHP 的 curl 中执行此操作?我试了又试,但一点运气都没有。

这是我获得访问 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);

我试过在这段代码之后做类似的事情,但是我没有运气。

$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)

我只需要弄清楚如何在 SalesForce 中创建销售线索,我不知道从哪里开始。任何帮助将不胜感激,因为我无法在任何地方找到对我有帮助的体面文档。

最佳答案

创建帐户演示,应该让你开始:

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;
}

关于php - 在 PHP 中使用 REST API 在 SalesForce 中创建销售线索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20335978/

相关文章:

php - 动态更改文本输入值

php - 使用 Doctrine ORM 统计表的所有记录

php - 错误设置证书验证位置 : CAfile ca-bundle. crt CApath:无

javascript - TypeError : this. state.student.map 不是一个函数

api - 使用 JSON 作为内容操作结果类型

c++ - 如何用C++实现支持HTTPS的RESTful Web Server?

json - BeanShell后处理器使用Jmeter解析Json

iOS 应用程序的 PHP is_uploaded_file 失败

api - 不是有效的 base64 图像

Java:内部类传值的线程安全