I'm working on a wordpress plugin, which pull data from another site via a cURL post call. I've tested the same code in i wordpress plugin and outsite of wordpress.
Outsite of wordpress the script works fine, but inside a wordpress plugin, the script just wont work:
Wordpress plugin:
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_POST, true );
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($handle, CURLOPT_POSTFIELDS, array('postfield' => 'postfieldcontent'));
$result = curl_exec($handle);
Main site:
mysql_query("INSERT INTO table (q, w, e, r, t, y, u) VALUES ('', '".$_POST[postfield]."', '', '', '', '', '')");
I've removed all database security for debugging purpose. I have also tried the wp_remote_post function, that doesn't work either. I've even triede the the wp_remote_get function, but i can access the get variable:
$result = wp_remote_get( 'http://qwerty.dk/folder/filename.php?getfield=qwertrert' );
I've given up - please help :)
Best regards
Kim
You will need to enable cURL in your php.ini file. See #1347146
wp_remote_post() uses a class called WP_Http that in turn can use one of three transport classes (see file class-http.php function _get_first_available_transport).
POST method will work with class WP_Http_Curl, but will not work with class WP_Http_Streams (the cURL fallback).
The alternative is to use wp_remote_get()
Related
i thought i would reach out to get some guidance on a little thing i am working on.
What i would like to do within Wordpress:
Call external API (with token header)
Get the results of the api and save it into a file in wpallimport's upload folder
I would assume i can just make a simple WP plugin and within the 'activate' hook for the plugin:
create a wp-cron (as i would like it to run every day) for the following:
$url = 'the-api-url';
$data = wp_remote_get( $url ,
array('headers' => array( 'Token' => 'tokenkey')
));
$jsonfile = $data['body'];
global $wp_filesystem;
if (empty($wp_filesystem)) {
require_once (ABSPATH . '/wp-admin/includes/file.php');
WP_Filesystem();
}
$file = '/wp-content/uploads/wpallimport/files/JSONFILE.JSON';
$wp_filesystem->put_contents($file, $jsonfile);
However i am not having success with the above (with the correct API url and token etc obviously)
Thanks in advance!
Marketo API is so confusing for a new user like myself. I have an email and name and would love to pass that to marketo. How do I do that?
To push a lead record to Marketo via the REST API, there are a couple of endpoints you can choose from:
Sync Leads at POST /rest/v1/leads.json
Push Lead at POST /rest/v1/leads/push.json
Import Leads at POST /bulk/v1/leads.json
Out of these three, the Sync Leads might be the easiest to use, as that requires the least amout of additional parameters.
Basically, you have to make a POST request to the
https://<MUNCHKIN_ID>.mktorest.com/rest/v1/leads.json?access_token=<ACCESS_TOKEN> url with your data sent in the body of the request.
You will find your MUNCHKIN_ID under Admin > Integration > Munchkin tab in your instance. While still in the admin area, you should also create an API user (or allow API access for your own user), set up a LaunchPoint service with that user for the REST API and finally request an –temporary, valid for 1 hour– access token to test the connection. The whole process is described in detail under the Authentication chapter of the REST API documentation.
Once you have a –still valid– access token, you can make the call above with your lead information provided in the following data structure:
{
"action":"createOrUpdate",
"lookupField":"email",
"input":[
{
"email":"collizo#4sky.com",
"firstName":"Collizo4sky"
},
// …more leads (up to 300) if needed
]
}
In case you use php, here is an example code:
$munchkinId = '123-ABC-456';
$accessToken = 'abcdefgh-1234-5678-abcd-12345678abcd:lon';
$url = "https://{$munchkinId}.mktorest.com/rest/v1/leads.json?access_token={$accessToken}";
$dataJSON = [
'action' => 'createOrUpdate',
'lookupField' => 'email',
'input' => [
[
'email' => 'collizo#4sky.com',
'firstName' => 'Collizo4sky',
],
],
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($dataJSON));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
/**
* Which should result in a response like this:
* /
{
"requestId":"4033#16612d185ad",
"result":[
{
"id":1042016,
"status":"created"
}
],
"success":true
}
/**/
I'm trying to post a comment to WordPress (4.7.0) using basic authentication as described in the documentation and in WP REST API: Setting Up and Using Basic Authentication.
However, I keep getting 401 errors back.
{"code":"rest_comment_login_required","message":"Sorry, you must be logged in to comment.","data":{"status":401}}
I activated the basic authentication plugin which then should turn this call into an authenticated call, right?
You need to add a filter to your theme's functions.php file:
add_filter( 'rest_allow_anonymous_comments', function ( $allow_anonymous, $request ) {
// ... custom logic here ...
return true; // or false to prohibit anonymous comments via post
}, 10, 2 );
Documentation: https://developer.wordpress.org/reference/hooks/rest_allow_anonymous_comments/
Go to your root WordPress folder like rootfolder/wp-includes/rest-api/endpoints
Open the file: class-wp-rest-comments-controller.php
Change the line:
$allow_anonymous = apply_filters( 'rest_allow_anonymous_comments', false , $request );
to
$allow_anonymous = apply_filters( 'rest_allow_anonymous_comments', true, $request );
That's all
I have read many questions similar to this,
but found no answer
i have to display customer name and cart items in wordpress blog
I have installed Mage Enabler .but always it is showing Invalid Url.
what can be valid url for local pc?
so i also have done like,
require_once ("../app/Mage.php");
umask(0);
Mage::app();
Mage::getSingleton("core/session", array('name' => 'frontend'));
$session = Mage::getSingleton("customer/session");
if($session->isLoggedIn())
{echo "YES";}
else {echo "NO";}
I always get "NO" .. :(
Can anyone please help me how to access magento session in wordpress
Instead of:
$session = Mage::getSingleton("customer/session");
Try:
$session=Mage::getSingleton('customer/session', array('name'=>'frontend') );
Good luck
I am trying to add language/translation support for my Wordpress plugin using POEdit software to create .po files, but the code isn't working and there are no screen error printing out.
My plugin is located in /plugins/site-status/ and the languages directory is located as /plugins/site-status/languages/. In /languages/ directory all .po files have names such as site-status-en_US.po (the unique identifier is called site-status)
Here is the language support code:
function status_language_init() {
load_plugin_textdomain( 'site-status', false, 'site-status/languages/' );
}
add_action('init', 'status_language_init');
Here is the test code for outputing the default/translated text:
echo _x( 'test', 'site-status' );
I very much look forward for your help!
Thanks in advance and with best wishes,
WHOAMi
To debug this, check the return value of load_plugin_textdomain(). And don’t rely on a directory name of your plugin. The user can change it.
$path = basename( dirname( __FILE__ ) ) . '/languages';
$lang_loaded = load_plugin_textdomain( 'site-status', FALSE, $path );
// die harder!
! $lang_loaded and die( $path . ' not found' );