I have a Mailchimp signup form on a client's website which was working fine until this week, I don't know what happened but when I submit a new email address it returns an error which I can't see in the debugger tool.
I don't know if it has anything to do with the code or if Mailchimp made changes to their system but here's the code used for the signup form, I haven't made this plugin so I don't know if anything is missing.
HTML
<form action="" class="c-mailchimp">
<input type="text" class="mce-validator" value="" style="display: none;">
<input type="email" class="with-button mce-EMAIL" placeholder="Votre courriel" required>
<button type="submit" class="stick-to-input mc-embedded-subscribe">
<i class="fa fa-paper-plane-o" aria-hidden="true"></i>
</button>
<div class="mce-responses">
<div class="c-mailchimp__response mce-error-response" style="display:none">Oups, une erreur est survenue..</div>
<div class="c-mailchimp__response mce-success-response" style="display:none">Merci de vous ĂȘtre inscrit !</div>
</div>
</form>
custom-mce.php
<?php
/*
Plugin Name: Agence CC
Plugin URI: http://wordpress.org/extend/plugins/agence-cc/
Version: 1
Author: Agence CC
Description:
Text Domain: agencecc
License: GPLv3
*/
add_action( 'wp_ajax_submitMailchimpNewUser', 'submitMailchimpNewUser' );
add_action( 'wp_ajax_nopriv_submitMailchimpNewUser', 'submitMailchimpNewUser' );
function submitMailchimpNewUser() {
require_once 'includes/mailchimp.php';
$newUserEmail = $_POST['email'];
$subscribeResult = syncMailchimp($apiKey, $listId, $newUserEmail);
echo $subscribeResult;
die();
}
?>
mailchimp.php
<?php
$apiKey = 'Hidden for security purpose';
$listId = 'Hidden for security purpose';
function syncMailchimp($apiKey, $listId, $email) {
$memberId = md5(strtolower($email));
$dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
$url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/'.$memberId;
$json = json_encode([
'email_address' => $email,
'status' => "subscribed",
]);
$headers[] = 'Content-Type: application/json';
$headers[] = 'Content-Length: ' . strlen($json);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlInfo = curl_getinfo($ch);
curl_close($ch);
if($httpCode===200) {
return 'true';
} else {
return 'false';
}
}
Inside the app.js
var initMailChimp = function() {
var $submitButton = $('.mc-embedded-subscribe');
$submitButton.click(function(event){
event.preventDefault();
var $email = $(this).closest('form').find('.mce-EMAIL');
var email = $email.val();
var success = $(this).closest('form').find('.mce-success-response');
var error = $(this).closest('form').find('.mce-error-response');
var validator = $(this).closest('form').find('.mce-validator').val();
if(validator!=='') {
error.hide();
success.slideDown(300);
$email.val('');
} else {
$.post(
ajaxurl,
{
'action': 'submitMailchimpNewUser',
'email': email
},
function(response){
if(response==='true') {
error.hide();
success.slideDown(300);
$email.val('');
} else {
success.hide();
error.slideDown(300);
}
}
);
}
});
};
Related
I have the following sample code to receive sms when a customer fills in the template on contact form 7 as follows (APIKey and SecretKey i was available at the sms service provider):
add_action( 'wpcf7_mail_sent', 'wp_sms_function' );
function wp_sms_function( $contact_form )
{
$form_id = $contact_form->id;
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$posted_data = $submission->get_posted_data();
if ($form_id==6)
{
$APIKey="XXXXXXX"; //Please fill in APIKey
$SecretKey="XXXXX"; //Please fill in SecretKey
$YourPhone=$posted_data['your-phone']; //your-phone on Contact Form 7
$ch = curl_init();
$SampleXml = "<RQST>"
. "<APIKEY>". $APIKey ."</APIKEY>"
. "<SECRETKEY>". $SecretKey ."</SECRETKEY>"
. "<ISFLASH>0</ISFLASH>"
. "<SMSTYPE>8</SMSTYPE>"
. "<CONTENT>".'Welcome to my website %%your-phone%% .'."</CONTENT>"
. "<CONTACTS>"
. "<CUSTOMER>"
. "<PHONE>". $YourPhone ."</PHONE>"
. "</CUSTOMER>"
. "</CONTACTS>"
. "</RQST>";
curl_setopt($ch, CURLOPT_URL, "http://rest.esms.vn/MainService.svc/xml/SendMultipleMessage_V4_post_json/" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $SampleXml );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
$result=curl_exec ($ch);
$xml = simplexml_load_string($result);
}
}
}//End contact form
I used the above code to try, but I can't receive sms when I fill out the cf7 form.Please help me. Thanks!
Link document API sms: https://esms.vn/SMSApi/ApiDetail#ApiSendSMSBrandname
I have a little project in which I try to fetch data from a domain and put this information in input fields.
The Curl function is good and working. However, the jQuery script if not working or filling the input fields. If I use $url = "http://domain..."; , all is working on page load but if I use an input field with a button and post form, the fields are empty. The curl is working and gives the full page back.
How I can load the script with the same button but after load the curl script?
Button:
<form action="" method="POST">
<label for="name">URLinput</label>
<input type="url" id="inf_endpoint" name="inf_endpoint" value="" />
<button type="submit" name="mytest">Test This</button>
</form>
What I have tried:
<?php
if(isset($_POST['mytest'])){
$url=$_POST['inf_endpoint'];
$agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_URL, html_entity_decode($url));
$data = curl_exec($ch);
if($result === false){
echo 'Curl error: ' . curl_error($ch);
}else{
echo 'All is good';
?>
<script>
jQuery.ajax({
url: '<?php echo site_url('admin/matches/manage'); ?>',
type: 'GET',
success: function(res) {
var data = jQuery.parseHTML(res);
jQuery(data).find('div.right').each(function(){
$('#date').val(jQuery(this).html());
});
var data = jQuery.parseHTML(res);
jQuery(data).find('div.team a:first').each(function(){
$('#team1').val(jQuery(this).html());
});
var data = jQuery.parseHTML(res);
jQuery(data).find('div.team a:nth-child(2)').each(function(){
$('#team2').val(jQuery(this).html());
});
var data = jQuery.parseHTML(res);
jQuery(data).find('div.match_head .left a:first').each(function(){
$('#league').val(jQuery(this).html());
});
var data = jQuery.parseHTML(res);
jQuery(data).find('div.score').each(function(){
$('#result').val(jQuery(this).html());
});
var data = jQuery.parseHTML(res);
jQuery(data).find('div.team_logo a:first').each(function(){
$('#logo1').val(jQuery(this).html());
});
var data = jQuery.parseHTML(res);
jQuery(data).find('div.oppo2 a:first').each(function(){
$('#logo2').val(jQuery(this).html());
});
}
});
</script>
<?php
}
curl_close($ch);
echo $data;
}
?>
But this is working on Page load. But not with a if statement with click
$url="https://thedomain";
$agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_URL, html_entity_decode($url));
$data = curl_exec($ch);
if($result === false){
echo 'Curl error: ' . curl_error($ch);
}else{
echo 'All is good';
}
curl_close($ch);
echo $data;
and
jQuery.ajax({
url: '<?php echo site_url('admin/matches/manage'); ?>',
type: 'GET',
success: function(res) {
var data = jQuery.parseHTML(res);
jQuery(data).find('div.right').each(function(){
$('#date').val(jQuery(this).html());
});
var data = jQuery.parseHTML(res);
jQuery(data).find('div.team a:first').each(function(){
$('#team1').val(jQuery(this).html());
});
var data = jQuery.parseHTML(res);
jQuery(data).find('div.team a:nth-child(2)').each(function(){
$('#team2').val(jQuery(this).html());
});
var data = jQuery.parseHTML(res);
jQuery(data).find('div.match_head .left a:first').each(function(){
$('#league').val(jQuery(this).html());
});
var data = jQuery.parseHTML(res);
jQuery(data).find('div.score').each(function(){
$('#result').val(jQuery(this).html());
});
var data = jQuery.parseHTML(res);
jQuery(data).find('div.team_logo a:first').each(function(){
$('#logo1').val(jQuery(this).html());
});
var data = jQuery.parseHTML(res);
jQuery(data).find('div.oppo2 a:first').each(function(){
$('#logo2').val(jQuery(this).html());
});
}
});
Without click function > The Page is loading and fill the fields.
cURL is a security risk even when your dealing with your servers but let me try to point out some items. Someone can get between you and your curl, magic can happen.
Your first line I don't think it's doing the proper check
<?php
if(isset($_POST['mytest'])){
change to
<?php
if(isset($_POST['Submit'])){
or even better to
if($_SERVER['REQUEST_METHOD']=='POST'){
Secondly, I can't see where $result is being set, change the following
if($result === false){
to
if(empty($data)){
I hope that solves missing points
Here is my PHP Code:
function post_captcha($user_response) {
$fields_string = '';
$fields = array(
'secret' => '',
'response' => $user_response
);
foreach($fields as $key=>$value)
$fields_string .= $key . '=' . $value . '&';
$fields_string = rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
$res = post_captcha($_POST['g-recaptcha-response']);
if (!$res['success']) {
die('MF005');
} else {
if ($mail->send()) {
$send_arEmail = $autoresponder->Send();
}}
and here is my JavaScript code:
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script>
function onSubmit(token) {
document.getElementById("i-recaptcha").submit();
}
</script>
This is my captcha:
<div class="g-recaptcha" data-sitekey="" ></div>
and this is button submit:
<button class="button rounded" type="submit" data-callback="onSubmit">Send</button>
This is also my form id:
<form id="i-recaptcha">
This above function is working, but after submit form, still captcha is checked, I want to reset it to unchecked once user click on submit button.
You can use it like this. It's based on version.
Version 3
grecaptcha.ready(function() {
grecaptcha.execute('SITE_KEY', {action: 'homepage'}).then(function(token){
$('#token').val(token);
});
});
Version 2
grecaptcha.reset();
Version 1
Recaptcha.reload();
I am one of the dejected ex-Mandrill subscribers who has suddenly had to find another email platform. I chose Mailgun but am beggining to regret this decision as it all seems to be way above my head.
I was able to use very basic javascript/php and Zurb Foundation's validate to create a simple contact form on my website but I'm having trouble figuring out where to begin as far as incorporating Mailgun's API and I can't seem to find any documentation that doesn't require I open Terminal or start from scratch...
jQuery:
$('#contact-form').on('valid.fndtn.abide', function() {
var name = $("input#name").val();
var email = $("input#email").val();
var message = $("textarea#message").val();
//Begin Ajax Call
$.ajax({
type: "POST",
url: "assets/php/mail.php",
data: {
'key': 'XXXXXXXX',
'message': {
'from_email': email,
'from_name': name,
'headers': {
'Reply-To': email
},
'subject': 'From My Site',
'text': message,
'to': [{
'email': 'me#me.com',
'name': 'me',
'type': 'to'
}]
}
},
success: function() {
$('#contact-form').html("<div id='success' class='large-6 large-offset-6 medium-12 columns'></div>");
$('#success').html("<h2 class='collapse'>Message recieved.</h2>")
.append("<p class='collapse'>Thanks " + name + ", I will be in touch with you shortly.</p>")
.hide()
.fadeIn(1500);
},
}); //ajax call
return false;
});
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$msg = "
Name: $name
Email: $email
Commments:
$message
";
$to = "me#me.com";
$subject = "From My Site";
$message = $msg;
mail($to,$subject,$message,$headers);
?>
For anyone with any Mailgun experience, where do I start? I just want people to be able to send me an email from my site, and would rather not have to switch to another platform like SendGrid and pay $10/mo to receive the odd email here and there. Thanks for any insight.
$config = array ();
$config ['api_key'] = "key-#########################";
$config ['api_url'] = "https://api.mailgun.net/v3/xyz.com/messages";
$message = array ();
$message ['from'] = "SITE NAME < feedback#xyz.com>";
;
$message ['to'] = $to_email;
$message ['h:Reply-To'] = "SITE NAME < feedback#xyz.com>";
;
$message ['subject'] = 'Test Subject';
$message ['html'] = '<b>Test Message</b>';
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $config ['api_url'] );
curl_setopt ( $ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_setopt ( $ch, CURLOPT_USERPWD, "api:{$config['api_key']}" );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $message );
$result = curl_exec ( $ch );
curl_close ( $ch );
echo $result;
Before using the above code you need to create mailgun account & generate api-key for you domain.
I'm trying to scrape data from our local government. What I want is address from kids adoption offices. Here, in Brazil, all adoptions go through the government. So I have the URL of one office, there are 2 or 3 thousands more. But if I can manage to get one, the others will be easy.
I made many attempts, bellow I show three.
The problem could be related to a Javascript (Ajax maybe) that refresh the page.
Note: I am not a PHP developer.
First attempt
echo '<html><head></head><body>';
echo '<h1>Scraper PHP GET 1</h1>';
echo ini_get("allow_url_fopen");
echo ini_get("allow_url_fopen");
// I used this url for test
//$url = 'http://www.portaldaadocao.com.br';
//This is the URL that I really want
$url = 'http://www.cnj.jus.br/cna/Controle/ConsultaPublicaBuscaControle.php?transacao=CONSULTA&vara=2673';
$html = file_get_contents($url);
var_dump($html);
echo '</body></html>';
// Output
// 11
// Warning:
file_get_contents(http://www.cnj.jus.br/cna/Controle/ConsultaPublicaBuscaControle.php?
transacao=CONSULTA&vara=2673) [function.file-get-contents]: failed to open stream: HTTP
request failed! HTTP/1.1 404 Not Found in /home/rsl/www/sc01_get.php on line 14
// bool(false)
Second attempt
echo '<html><head></head><body>';
echo '<h1>Scraper PHP CURL 3</h1>';
// I used this url for test
//$url = 'http://www.portaldaadocao.com.br';
//This is the URL that I really want
$url = 'http://www.cnj.jus.br/cna/Controle/ConsultaPublicaBuscaControle.php?transacao=CONSULTA&vara=2673';
$curl = curl_init($url);
#curl_setopt($curl, CURLOPT_POSTFIELDS, "foo");
#curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
#curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");;
$html=#curl_exec($curl);
if (!$html) {
echo "<br />cURL error number:" .curl_errno($curl);
echo "<br />cURL error:" . curl_error($curl);
exit;
}
else{
echo '<br>begin HTML[';
echo $html;
echo '<br>]end html ';
}
echo '</body></html>';
// Output
// 1
third attempt
function curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.6 (KHTML, like Gecko) Chrome/16.0.897.0 Safari/535.6');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_REFERER, "http://www.windowsphone.com");
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo '<html><head></head><body>';
echo '<h1>Scraper PHP CURL 5</h1>';
// I used this url for test
//$url = 'http://www.portaldaadocao.com.br';
//This is the URL that I really want
$url = 'http://www.cnj.jus.br/cna/Controle/ConsultaPublicaBuscaControle.php?transacao=CONSULTA&vara=2673';
$curl = curl_init($url);
#curl_setopt($curl, CURLOPT_POSTFIELDS, "foo");
#curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
#curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");;
$html=#curl($curl);
if (!$html) {
echo "<br />cURL error number:" .curl_errno($curl);
echo "<br />cURL error:" . curl_error($curl);
exit;
}
else{
echo '<br>begin HTML[';
echo $html;
echo '<br>]end html ';
}
echo '</body></html>';
// Output
// cURL error number:0
// cURL error: