【发布时间】:2015-03-05 20:53:43
【问题描述】:
我正在尝试使用来自 Salesforce 的数据填充 WordPress 站点。有点类似于this question,可能只是我走的方向相反。我的代码相当多地基于this example,当我自己尝试它时它对我来说效果很好,但一旦我用Wordpress尝试它就没有那么多了。
从示例中,我只用下面的代码替换了 demo_rest.php,更改了 oauth_callback.php 中的最后一行以返回到我在 WordPress 中的选项页面,正确更改为 config.php,并替换了 index.html。当我这样做时,我在 Salesforce 中看到我确实是从我的插件登录的,但没有任何东西从那里返回到 WordPress。它没有显示在我想要的窗口中,也没有使用应该产生的 json 更新 mysql 数据库。我做错了什么?
我的代码。
<?php
/*Assign global variables*/
$plugin_url = WP_PLUGIN_URL . '/my-plugin';
$options = array();
// adds plugin settings page in wordpress admin menu
function salesforce_menu(){
add_options_page( // wordpres helper function to add a settings page
'Salesforce to WordPress Integrator',
'Salesforce Integration',
'manage_options',
'salesforce-integrator',
'salesforce_options_page'
);
}
add_action('admin_menu','salesforce_menu');
function salesforce_options_page(){
if ( !current_user_can('manage_options')){
wp_die ('You do not have sifficient permissions to access this page.');
}
global $plugin_url;
global $options;
if( $access_token != '') {
$response = get_stories($instance_url, $access_token);//This is where I'm probably getting messed up. I'm trying to call the get_stories function (defined later)
$options['response'] = $response;
$options['last_updated'] =time();
update_option ('storybank_approved', $options); //wp helper function to update the options table, hopefully with the results of my call to the API.
}
$options = get_option('storybank_approved');
if ($options !=''){
$response = $options['response'];
}
var_dump ($response); //I want this to show me the json on my settings page just to ensure that I have a response, but it looks like I don't
require('includes/options-page-wrapper.php');
}
//get salesforce records
function get_stories($instance_url, $access_token) {
$query = "SELECT Id, Name from salesforce_Object__c LIMIT 100";
$url = "$instance_url/services/data/v20.0/query?q=" . urlencode($query) ;
$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"));
$json_response = curl_exec($curl);
curl_close($curl);
$response =json_decode($json_response,true);
$total_size = $response['totalSize'];
echo "$total_size record(s) returned<br/><br/>";
foreach ((array) $response['records'] as $record) {
echo $record['Id'] . ", " . $record['Name']. "<br/>";
}
echo "<br/>";
}
?>
【问题讨论】:
标签: php wordpress curl salesforce integration