0

I am trying to integrate a third party API (positionly.com) into a WordPress member site. I am using the following code to add a positionly level to the corresponding membershipmouse member level

$base_url = POSITIONLY_API_URL;$url = $base_url . '/accounts.json'; 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
  $headers = array (
    "Connection: keep-alive",
    "Accept: application/json",
    "Token: " . POSITIONLY_TOKEN
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);

$user_level = mm_member_data(array("name"=>"membershipId"));
    $plan = '');
    if($user_level = 2){
        $plan = "ClickMinded Bronze";
    } elseif ($user_level = 3){
        $plan = "ClickMinded Silver";
    }elseif($user_level = 4){
        $plan = "ClickMinded Gold";
    }
    // assign membermouse custom field a value
    $userdata['cf_1'] = $plan;

     //sets the positionly values to equal the membermouse values
    $post_params['owner_attributes[email]'] = $userdata['email'];
    $post_params['owner_attributes[name]'] = $userdata['first_name'] . ' '.$userdata['last_name'];
    $post_params['owner_attributes[password]'] = $user_password;
    $post_params['owner_attributes[plan_name]'] = $userdata['cf_1'];


    $post_string = http_build_query($post_params);

    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
    $result=curl_exec ($ch);
    curl_close ($ch);

// it breaks somewhere in here according to var_dump($result) the positionly plan always comes up as ClickMinded Silver
   $json = json_decode($result);
     if( $json->status == "ok" ){

    add_user_meta($userdata['member_id'], 'positionly_token', $json->token);
    add_user_meta($userdata['member_id'], 'positionly_id', $json->account->id);
    add_user_meta($userdata['member_id'], 'positionly_plan', $json->account->plan));

Have been in touch with the support at positionly and membermouse.

Have looked through several articles here and elsewhere "using php with json". Such as Get value of JSON object in PHP variable Using PHP Variables in Javascript with JSON

I have tried json_encoding the $plan variable I have gone through each step with var_dump and get the correct $plan -> level but it will not transfer to the json results.

I am at a loss. Any suggestions would be much appreciated

Community
  • 1
  • 1
Ernie
  • 1
  • 1
  • Where is your `curl_init` at which `url` you are doing `curl` ? – Raheel Apr 07 '15 at 07:41
  • Not sure what you are asking here. JSON is not a scripting language, so I don't know how you would "use php variable IN JSON". What does `var_dump($result)` show? What does `var_dump($json)` show? – Mike Brant Apr 07 '15 at 20:51
  • Mike yeah I worded that wrong. What I am trying to do is insert a positionly plan 'Gold, Silver, or Bornze' into a corresponding membership level created in WordPress using Membermouse. The trouble I am having is switching the positionly plan. They have a default plan silver, when I run var_dump($json) or var_dump($results) I get the default silver plan. not the plan the user selected – Ernie Apr 07 '15 at 21:34

2 Answers2

0

You are not initializing curl properly. You should provide the url to hit and also if you don't set the CURL_RETURNTRANSFER to true curl will not return the result instead it will just return true.

Change your code like :

$ch = curl_init('https://auth.positionly.com/oauth2/token');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURL_RETURNTRANSFER, true);
$result=curl_exec ($ch);
curl_close ($ch); 
Raheel
  • 8,716
  • 9
  • 60
  • 102
  • Thanks for the reply Raheel, I just added the cURL init. I have inherited this project and assumed that part was done correctly. From the little documentation I have from positionly I was thinking it was in the JSON not switching but will try your suggestion – Ernie Apr 07 '15 at 20:42
  • `curl_setopt($ch, CURL_RETURNTRANSFER, true);` this line is must when you expect result from curl – Raheel Apr 08 '15 at 07:46
0

It was in the JSON had to add a new function, that used positionly's switch.json

function lina_update_user_plan($id, $plan){
$base_url = POSITIONLY_API_URL;

$url = $base_url . '/accounts/'.$id.'/switch.json';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);

$headers = array (
                  "Connection: keep-alive",
                  "Accept: application/json",
                  "Token: " . POSITIONLY_TOKEN
                  );

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);

$post_params['plan'] = $plan;

$post_string = http_build_query($post_params);

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);

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

$json = json_decode($result);

if( $json->status == "ok" ){
    return true;
}
return false; }

Then we had to call the new function in the original function so if there was a status change it would add or update the user depending on the case

function lina_mm_member_add($userdata){
$base_url = 'https://api.positionly.com/v2/clickminded';
$base_url = POSITIONLY_API_URL;

$url = $base_url . '/accounts.json';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);

$headers = array (
    "Connection: keep-alive",
    "Accept: application/json",
    "Token: " . POSITIONLY_TOKEN
);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);
 $_REQUEST['mm_field_last_name']; 
 $user_password =    $_REQUEST['mm_field_password'];

    $plan = '';

    $user_level = $userdata['membership_level'];

    if($user_level == 2){
        $plan = "clickminded-bronze";
    } elseif ($user_level == 3){
        $plan = "clickminded-silver";
    }elseif($user_level == 4){
        $plan = "clickminded-gold";
    }

$post_params['owner_attributes[email]'] = $userdata['email'];
$post_params['owner_attributes[name]'] = $userdata['first_name'] . ' '. $userdata['last_name'];
$post_params['owner_attributes[password]'] = $user_password;
$post_params['owner_attributes[plan_name]'] = $plan;

$post_string = http_build_query($post_params);

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);

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

$json = json_decode($result);
   if( $json->status == "ok" ){
    add_user_meta($userdata['member_id'], 'positionly_token', $json->token);
    add_user_meta($userdata['member_id'], 'positionly_id', $json->account->id);

    //Update user's plan:
    $ret = lina_update_user_plan($json->account->id, $plan);

    if( $ret == true )
        add_user_meta($userdata['member_id'], 'positionly_plan', $plan);
    else
        add_user_meta($member_id, 'positionly_switch_plan_status', "Switch Failed");


}
Ernie
  • 1
  • 1