Send request to other server in wordpress - wordpress

I have two website in wordpress
Main site
1.)http://www.abc.com
Other Site
2.)http://www.xyz.com
From mainsite I want to send username and password to othersite for this i am using this code
$creds = array();
echo $creds['user_login'] = "sachin";
echo $creds['user_password'] = "pass";
$user = wp_signon( $creds, false );
// echo ABSPATH . WPINC;
if ( is_wp_error($user) ){
echo $user->get_error_message();
if( !class_exists( 'WP_Http' ) )
include_once( ABSPATH . WPINC. '/class-http.php' );
// You would edit the following:
$username = $creds['user_login']; // Twitter login
$password = $creds['user_password']; // Twitter password
$message = "Hey neat, I'm posting with the API";
// Now, the HTTP request:
$api_url = 'http://www.xyz.com';
$body = array( 'status' => $message );
$headers = array( 'Authorization' => 'Basic '.base64_encode("$username:$password") );
$request = new WP_Http;
$result = $request->request( $api_url , array( 'method' => 'POST', 'body' => $body, 'headers' => $headers ) );
// echo"<pre>";print_r($result['body']);
I dont know i am sending wp http request in right way or not but my problem is i want this username and password(i am sending from main site) to other server (http://www.xyz.com)
How I can do that please suggest me.please provide me your valuable suggestion how I can send username and password from one server to other server.
Thanks

To share 2 wordpress installs so that users can access both then this page will let you do that.
http://www.remicorson.com/share-users-database-on-different-wordpress-installs/
I think this is what your trying to do?

Related

How update ACF field in savepost hook and wp remote post

How I can update post meta or ACF fields by wp_remote_post or other method by using exists API ?
function publish_content($post_id, $post, $update)
{
// If this is just a revision, don't send the email.
if (wp_is_post_revision($post_id)) {
return;
}
$full_content = get_field('full_content', $post_id);
$login = 'username';
$password = 'password';
$response = wp_remote_post(
'https://example.com/wp-json/wp/v2/posts/12',
array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode("$login:$password")
)
)
);
//I need Update ACF Field Value in example.com site
}
add_action('save_post', 'publish_content', 11, 3);
I don't want to develop new API on the destination site and I want to use the existing (wp/v2/posts/{id}) WordPress API.

Wordpress does not send a POST request with data

I need to export woocommerce order data using a POST request to a third-party url.I have this code in functions.php
add_action('woocommerce_thankyou', 'wdm_send_order_to_ext');
function wdm_send_order_to_ext ( $order_id ){
// get order object and order details
$order = new WC_Order( $order_id );
$email = $order->get_billing_email();
$phone = $order->get_billing_phone();
$shipping_type = $order->get_shipping_method();
$shipping_cost = $order->get_total_shipping();
// set the address fields
$user_id = $order->get_user_id();
$address_fields = array('country',
'title',
'first_name',
'last_name',
'company',
'address_1',
'address_2',
'address_3',
'address_4',
'city',
'state',
'postcode');
$address = array();
if(is_array($address_fields)){
foreach($address_fields as $field){
$address['billing_'.$field] = get_user_meta( $user_id, 'billing_'.$field, true );
$address['shipping_'.$field] = get_user_meta( $user_id, 'shipping_'.$field, true );
}
}
// set the username and password
$api_username = 'username';
$api_password = 'password';
// setup the data which has to be sent
$data = array(
'username' => $api_username,
'password' => $api_password,
'name1' => $address['billing_first_name'],
'street' => $address['billing_address_1'],
'city' => $address['billing_city'],
'country' => $address['billing_country'],
'pcode' => $address['billing_postcode'],
'phone' => $phone,
'weight' => 4.0,
'parcel_type' => 'D',
'num_of_parcel' => 1
);
// send API request via cURL
$curl = curl_init();
/* set the complete URL, to process the order on the external system. Let’s consider http://example.com/buyitem.php is the URL, which invokes the API */
curl_setopt($curl, CURLOPT_URL, "https://easyship.si/api/parcel/parcel_import");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec ($curl);
curl_close ($curl);
}
However, it does not pass the request, the third-party API does not receive anything. WP_DEBUG doesn't see any errors. Tell me where to look, what is wrong?
You can use https://requestbin.com/ as endpoint to see data you are posting and if they are being posted correctly. If it successfully posting it to requestbin endpoint you can see the header and body formats as well. If its not working try debugging line by line.
Sorry I wasn't able to add this as comment

How to submit contact form 7 programmatically

I want to submit contact form by custom function
The code below is getting the instance of form but when submitted. It submit the form but not the fields which I wanted.
$item = wpcf7_contact_form( $formId );
$result = $item->submit();
Here where I can pass the fields I define in admin panel like "textarea-123" & "email-234" ?
I did not get exact answer for what I look but I found the alternate solution.
function cf7Submit($formId , $args) {
$url = 'http://example.com/wp-json/contact-form-7/v1/contact-forms/'.$formId.'/feedback';
$response = wp_remote_post( $url, array(
'method' => 'POST',
'body' => $args
)
);
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>';
}
}
I can call this function like this:
cf7Submit(128, array(
'textarea-123' => 'test email',
'email-234' => 'asd#asd.com'));
#daraptoor has found a good solution, but as #davevsdave noticed in the comment, it does not work properly in CF7 5.6.
Error 415 is caused by added to API check for content type passed into a request header:
// part of create_feedback() from CF7's rest-api.php
if ( ! str_starts_with( $content_type, 'multipart/form-data' ) ) {
To figure it out, just add the expected content type into a request header:
$response = wp_remote_post( $url, array(
'method' => 'POST',
'headers' => array(
'Content-Type' => 'multipart/form-data'
),
'body' => $args
)
);
UPD
Faced with an issue, that wp_remote_post() send data in body and not in POST, so CF7 API does not get any fields. It is caused because the WP's function uses http_build_query() (read more here).
I have used cURL request as a workaround:
// Same user agent as in regular wp_remote_post().
$userAgent = 'WordPress/' . get_bloginfo('version') . '; ' . get_bloginfo('url');
// Note that Content-Type wrote in a bit different way.
$header = ['Content-Type: multipart/form-data'];
// Same array with fields to pass, not changed.
$body = ['foo' => 'bar'];
$curlOpts = [
// Send as POST
CURLOPT_POST => 1,
// Get a response data instead of true
CURLOPT_RETURNTRANSFER => 1,
// CF7 will reject your request as spam without it.
CURLOPT_USERAGENT => $userAgent,
CURLOPT_HTTPHEADER => $header,
CURLOPT_POSTFIELDS => $body,
];
$ch = curl_init($apiUrl); // Create a new cURL resource.
curl_setopt_array($ch, $curlOpts); // Set options.
$response = curl_exec($ch); // Grab response.
if (!$response) {
// Do something if an error occurred.
} else {
$response = json_decode($response);
// Do something with the response data.
}
// Close cURL resource, and free up system resources.
curl_close($ch);
Hope it saves someones time :)
You can add a piece of JS code, like:
$("form.wpcf7").submit()

How to send an email to a mailchimp Automation on wordpress post publish

In wordpress, I'm trying to POST a specific email included as a value of a custom field (called customer_email) in each post to a mailchimp automation.
Below is the code I have and it's not working
function email_on_publish( $postid ) {
$api_key = API-KEY;
$workflow_id = WORKFLOW-ID;
$email_id = EMAIL-ID;
$email = get_post_meta( get_the_ID(), 'customer_email', true );
$args = array(
'method' => 'POST',
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'user:'. $api_key )
),
'body' => json_encode(array(
'email_address' => $email
))
);
$response = wp_remote_post( 'https://' . substr($api_key,strpos($api_key,'-')+1) . '.api.mailchimp.com/3.0/automations/' . $workflow_id . '/emails/' . $email_id . '/queue/' . md5(strtolower($email)), $args );
$body = json_decode( $response['body'] );
}
add_action( 'pending_to_publish', 'email_on_publish' );
add_action( 'draft_to_publish', 'email_on_publish' );
Update: This has been resolved. There were two issues at hand.
I didn't need "md5(strtolower($email)" at the end of the post url.
Mailchimp requires you to send all prior emails in the workflow to the user before adding them to an automation. This was rendering all of my testing useless until I learned that.
Thanks for figuring this out, self!

Facebook page tab changing

I want to change tab name of a facebook page.
Here is the code I am using
<?php
session_start();
$pageId=$_SESSION['pageid'];
require('sdk/facebook.php');
$appId = 'My App Id';
$secret = 'My App Secret';
$pageId = $pageId;
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $secret,
));
$access_token=$facebook->getAccessToken();
if($facebook->setAccessToken($access_token))
{
$page_tabs=$facebook->api($pageId . '/tabs');
$name=$page_tabs['data']['0']['name'];
$tabid=$page_tabs['data']['0']['id'];
if($name=="MyTab")
{
$facebook->setAccessToken($access_token);
$facebook->api($tabid, 'POST', array(
'custom_name' => 'MyTab New Name',
'access_token'=>$_SESSION['token']
));
}
echo "OK";
}
?>
But I am getting error for Oauth Exception , it says you need to supply the accesstoken for this
Please Help
you need few changes
Instead of this
'access_token'=>$_SESSION['token']
use this
//get the user access token
$token = $facebook->getAccessToken();
echo "</br>" . 'User Access_Token:' . $atoken;

Resources