Get session from another domaine - php

I have a wordpress web site and Phalcon web application on another server. When I'm logged on my web application I have a session. When I'm back on wordpress I want to check if there is a session on my web application and if it's true I'll log the user on wordpress too.
So I thought I'll create a web service in my web application and it will called by wordpress. In this web service I'll check if the session exists and return the email user to log wordpress side.
But I have a problem because when I use the web service with wordpress, the session does not exist. But when I enter the URL via the browser, it works.
Could you help me to do that ?
Wordpress side
add_action( 'init', 'process_post' );
function process_post() {
$url = 'http://exemple.com/webapp/controller/logged';
$response = wp_remote_post( $url, array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(),
'body' => array( 'from' => 'authentication' ),
'cookies' => array()
)
);
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
echo "Something went wrong: $error_message";
} else {
echo 'Response:<pre>';
print_r( $response );
echo '</pre>';
}
}
Phalcon Web application web service
public function loggedAction(){
$this->view->disable();
$auth = $this->session->get('my_session');
if($auth){
$jsonResponse = array('authorization' => 'OK');
return $this->jsonBuilder($jsonResponse);
}
}
This is not the whole code phalcon side but this code can be good just for testing

Related

wordpress wp_remote_post Get the Response from another wordpress site

Everyone, I am New for Wordpress Development I develop one plugin there is two Wordpress site. site 1 sends the request to site 2. And site 2 will send some response for site 1 requests
site 1 index.php look like
$url = 'http://localhost/wordpress1/';
$get_post_id = $commentdata['comment_details']['comment_post_ID'];
$response = wp_remote_post( $url, array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(),
'body' => array('comment_details'=>$commentdata,'post_details'=>$post_details),
'cookies' => array()
));
$response_data = $response['body'];
site 2: index.php file looks like
$data = //some array data i will return
$sample = serialize($data);
output: Its works good
my Requirement:
we have written the code both sites but t want to manage all those things in site 1 plugin code how to connect both code single plugin page(request and response handle in the single plugin)

WordPress HTTP API

I'm trying to get points from a plugin called myCRED and have those points displayed on another site. This is possible because myCRED has a remote API that works through the WordPress HTTP API. I have the code that allows me to access the API but I'm confused how to use it to echo the point results on to the page. Here's what I have so far
$secret_key = 'mysecretkey';
$remote_url = 'http://siteb.com/api/';
$action = 'GET';
$account = 'john.doe#email.com';
$point_type = 'my_custom_type';
$host = get_bloginfo( 'url' );
$token = md5( $host . $action . $secret_key );
$request = array(
'method' => 'POST',
'body' => array(
'action' => $action,
'account' => $account,
'type' => $point_type,
'token' => $token,
'host' => $host
)
);
$response = wp_remote_post( $remote_url, $request );
How do I get my points to echo on to the page? Where do I put this code?
Correct me if i'm wrong here but you currently have two wordpress websites. One is using a plugin myCRED which has an api. You want to use that api from another wordpress website to display information.
You should handle the templating on the client side. I've included an example for enqueuing a script with an nonce and ajax url exposed to be able to communicate to your wordpress backend.
Using wp_ajax, you can create a function which will return your data from the myCRED api and then return that data back to client side who requested it.
The templating should be handled client side which I left for you since I'm unsure of the end goal here.
This example could be turned into a very simple plugin, but for brevity sakes I've shown to just include this in your functions.php file
Wordpress Enqueue Script
https://developer.wordpress.org/reference/functions/wp_enqueue_script/
// functions.php
function my_cred_script() {
wp_register_script(
'cred-script',
get_template_directory_uri() . '/path/to/js/file.js',
array('jquery'),
null,
true
);
wp_enqueue_script( 'cred-script' );
wp_localize_script( 'cred-script', 'ajax_object', array(
'cred_nonce' => wp_create_nonce( 'cred_nonce' ),
'ajax_url' => admin_url( 'admin-ajax.php' )
)
);
}
Wordpress Ajax Reference
https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_(action)
// functions.php
function get_my_cred_data() {
if(!isset( $_POST['cred_nonce'] ) ||
!wp_verify_nonce( $_POST['cred_nonce'], 'cred_nonce' )) {
die();
}
$secret_key = 'mysecretkey';
$remote_url = 'http://siteb.com/api/';
$action = 'GET';
$account = 'john.doe#email.com';
$point_type = 'my_custom_type';
$host = get_bloginfo( 'url' );
$token = md5( $host . $action . $secret_key );
$request = array(
'method' => 'POST',
'body' => array(
'action' => $action,
'account' => $account,
'type' => $point_type,
'token' => $token,
'host' => $host
)
);
$response = wp_remote_post( $remote_url, $request );
echo $response;
die();
}
add_action('wp_ajax_cred_data', 'get_my_cred_data');
add_action('wp_ajax_nopriv_cred_data', 'get_my_cred_data');
In some javscript file in your theme that was enqueued from the above. We can now call our action ('cred_data') we created in wordpress from the client side. We should get back some json and can template it however we like.
function getCREDData() {
var options = {
action: 'cred_data',
cred_nonce: ajax_object.cred_nonce
};
$.post(ajax_object.ajax_url, options)
.then(function(response) {
// handle response
})
.catch(function(err) {
// handle err
});
}

WordPress REST API Basic Authentication

I am working on a project that requires some integration between different WordPress instances and I am in the process of creating a WordPress plugin that will provide that functionality through the REST API.
I have enabled the WP-API plugin and the Basic Authentication plugin and am able to make requests that do not require authentication but when I make a request that does require authentication, such as adding a new page, I am met with 401 - Sorry, you are not allowed to create new posts.
I realize basic authentication is not suitable for production needs but would like to get it working properly for development and have been spinning my wheels on this seemingly small problem. I am perfectly able to make these requests using Postman, so there is something wrong with my implementation. Here is the code in question:
function add_new_page($post) {
// Credentials for basic authentication.
$username = 'user';
$password = 'password';
// Request headers.
$headers = array(
'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password ),
'Content-Type' => 'application/json'
);
// Request URL.
$url = "http://localhost/wp-json/wp/v2/pages";
// Request body.
$body = array(
'slug' => $post->post_name,
'status' => $post->post_status,
'type' => $post->post_type,
'title' => $post->post_title,
'content' => $post->post_content,
'excerpt' => $post->post_excerpt,
);
$body_json = json_encode($body);
// Request arguments.
$args = array(
'method' => 'POST',
'blocking' => true,
'headers' => $headers,
'cookies' => array(),
'body' => $body_json,
);
// Fire request.
$response = wp_remote_request($url, $args);
// Handle response.
if (is_wp_error($response)) {
$error_message = $response->get_error_message();
echo "Something went wrong: $error_message";
} else {
$response_body = json_decode(wp_remote_retrieve_body($response));
// Display response body.
echo '<pre>';
print_r($response_body);
echo '</pre>';
}
// Exit so we can read the response.
exit();
}
I would be really appreciative of any insights somebody out there could provide.

How to Integrate 3rd party API in Wordpress

I'm using wordpress and i want to integrate an SMS API into my wordpress site. Can anyone help in knowing where (in which file) to write the code for integration and also the code to integrate SMS API.
My SMS API Url is :
http://www.elitbuzzsms.com/app/smsapi/index.php?key=KEY&campaign=****&routeid=**&type=text&contacts=< NUMBER >&senderid=SMSMSG&msg=< Message Content >
I want to integrate above API in my wordpress theme so that i can send sms based on mobile number and add required message.
In wordpress you can use wp_remote_get and wp_remote_post
get request example
$url = 'http://www.elitbuzzsms.com/app/smsapi/index.php?key=KEY&campaign=****&routeid=**&type=text&contacts=< NUMBER >&senderid=SMSMSG&msg=< Message Content >';
$response = wp_remote_get( $url );
if( is_array($response) ) {
$header = $response['headers']; // array of http header lines
$body = $response['body']; // use the content
}
post request example
$response = wp_remote_post( $url, array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(),
'body' => array( 'username' => 'bob', 'password' => '1234xyz' ),
'cookies' => array()
)
);
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
echo "Something went wrong: $error_message";
} else {
echo 'Response:<pre>';
print_r( $response );
echo '</pre>';
}

Laravel Json response function

I am currently developing an application in Laravel (v4.2.11) which is using ExtJS (v4.2.1-gpl).
As part of my ExtJS application, I am developing a JSON response that is used by ExtJS. However, I want to do the following thing:
return Response::json(array(
'menusystem' => array(
'listeners' => array(
'click' => function() {
location.href = 'test'
}
)
)
);
I do know this is not valid JSON. However, this is the way how the previous developer of the application did it that way. I'd like to know if this is possible within PHP, Laravel or JSON.
You can do something like this
$response = array(
'menusystem' => array(
'listeners' => array(
'click' => "%%%function() {
location.href = 'test'
}%%%"
)
)
);
$response = json_encode($response);
$response = str_replace('"%%%', '', $response);
$response = str_replace('%%%"', '', $response);
return $response;
It's only general conception. You can detect function structure in special macro etc.

Categories