简体   繁体   English

在PHP中使用cURL登录Salesforce

[英]Log in Salesforce using cURL in PHP

I'm building a login page from where I would like to connect to my salesforce.com account. 我正在建立一个登录页面,从这里我想连接到我的salesforce.com帐户。 The idea is that if the username/password is wrong, I leave the user on my website and otherwise I redirect him to the salesforce panel. 想法是,如果用户名/密码错误,我会将用户留在我的网站上,否则我会将其重定向到salesforce面板。

To check if the username password exists, I use the Salesforce PHP Toolkit. 要检查用户名密码是否存在,我使用Salesforce PHP Toolkit。 And when I see that the user has entered a legit account, I use cURL to post the username and password to the form that is supposedly handling the login. 当我看到用户输入了合法帐户时,我使用cURL将用户名和密码发布到应该处理登录的表单中。

Here's some bits of the code: 这是一些代码:

$url = 'https://login.salesforce.com/';
$fields = array(
      'username' => urlencode($username),
      'pw' => urlencode($pw),
      'un' => urlencode($username),
      'width' => urlencode($width),
      'height' => urlencode($height),
      'hasRememberUn' => urlencode(true),
      'startURL' => urlencode(''),
      'loginURL' => urlencode(''),
      'loginType' => urlencode(''),
      'useSecure' => urlencode(true),
      'local' => urlencode(''),
      'lt' => urlencode('standard'),
      'qs' => urlencode(''),
      'locale' => urlencode('uk'),
      'oauth_token' => urlencode(''),
      'login' => urlencode(''),
      'serverid' => urlencode(''),
      'display' => urlencode('page')
  );

  $fields_string =''; 
  foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
  rtrim($fields_string,'&');
  $ch = curl_init();

  /* Set the url, number of POST vars, POST data */
  curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  curl_setopt($ch, CURLOPT_URL,$url);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
  curl_setopt($ch, CURLOPT_POST,count($fields));
  curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

  $result = curl_exec($ch);
  curl_close($ch);

I inspected the source of the login page of salesforce.com to see what post values it needs which I put it in the code above. 我检查了salesforce.com登录页面的源,以查看它需要什么帖子值,并将其放在上面的代码中。

The problem is that whenever I input a valid account, it redirects me to the login page (so I am not logged in) of salesforce.com saying 问题是,每当我输入有效的帐户时,它会将我重定向到salesforce.com的登录页面(因此我没有登录)

You have attempted to access a page that requires a salesforce.com login. 您试图访问需要salesforce.com登录的页面。 If you are already a user of the system, please login below. 如果您已经是该系统的用户,请在下面登录。

Could anyone tell me what I'm doing wrong ? 谁能告诉我我在做什么错?

Cheers ! 干杯!

urlencode() is not the proper method to encode your data with, since you're not passing the data in a URL. urlencode()不是正确的数据编码方法,因为您没有在URL中传递数据。

CURL can take an array and automatically convert the data in it to a POST-able data set. CURL可以采用一个数组,并将其中的数据自动转换为可发布POST的数据集。 You don't have to do the encoding yourself. 您不必自己进行编码。 Simply do 简单地做

curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

CURLOPT_POST takes a bool, not the number of fields you're posting. CURLOPT_POST需要一个布尔值,而不是您要发布的字段数。

curl_setopt($ch, CURLOPT_POST, 1);

Additionally, if the service uses cookies for session storage, you'll need to add a cookie jar. 另外,如果该服务使用cookie进行会话存储,则需要添加一个cookie jar。 From http://curl.haxx.se/libcurl/php/examples/cookiejar.html 来自http://curl.haxx.se/libcurl/php/examples/cookiejar.html

For the initial authentication request: 对于初始身份验证请求:

curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookieFileName");

And for subsequent requests using it: 对于使用它的后续请求:

curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookieFileName");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM