i'm sorry for my english.
I have a problem with the function wp_insert_post in a script.
I'm trying to create a new post when i receive a notification from paypal. Everything works fine untill i try to create a new post in the database.
This is my code:
<?php include_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
// Send an email announcing "received IPN"
$mail_From = "[email protected]";
$mail_To = "[email protected]";
$mail_Subject = "received IPN";
$mail_Body = $req;
mail($mail_To, $mail_Subject, $mail_Body, $mail_From);
?>
<?php
// STEP 1: read POST data
// Reading POSTed data directly from $_POST causes serialization issues with array data in the POST.
// Instead, read raw POST data from the input stream.
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval) {
$keyval = explode ('=', $keyval);
if (count($keyval) == 2)
$myPost[$keyval[0]] = urldecode($keyval[1]);
}
// read the IPN message sent from PayPal and prepend 'cmd=_notify-validate'
$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc')) {
$get_magic_quotes_exists = true;
}
foreach ($myPost as $key => $value) {
if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
$value = urlencode(stripslashes($value));
} else {
$value = urlencode($value);
}
$req .= "&$key=$value";
}
// STEP 2: POST IPN data back to PayPal to validate
$ch = curl_init('https://www.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
// In wamp-like environments that do not come bundled with root authority certificates,
// please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set
// the directory path of the certificate as shown below:
// curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
if( !($res = curl_exec($ch)) ) {
// error_log("Got " . curl_error($ch) . " when processing IPN data");
curl_close($ch);
exit;
}
curl_close($ch);
// STEP 3: Inspect IPN validation result and act accordingly
if (strcmp ($res, "VERIFIED") == 0) {
// Send an email announcing "enter in verified IF"
$mail_From = "[email protected]";
$mail_To = "[email protected]";
$mail_Subject = "enter in verified IF";
$mail_Body = $req;
mail($mail_To, $mail_Subject, $mail_Body, $mail_From);
// You should validate against these values.
$donCause = $_POST['item_number'];
$txnID = $_POST['txn_id'];
$firstName = $_POST['first_name'];
$lastName = $_POST['last_name'];
$addressCountry = $_POST['address_country'];
$addressCity = $_POST['address_city'];
$addressStreet = $_POST['address_street'];
$addressZip = $_POST['address_zip'];
$payerEmail = $_POST['payer_email'];
$payment_gross = $_POST['mc_gross'];
$payment_status = $_POST['payment_status'];
if ($payment_status == 'Completed') {
// Send an email announcing "enter in payment_status==completed"
$mail_From = "[email protected]";
$mail_To = "[email protected]";
$mail_Subject = "enter in payment_status==completed";
$mail_Body = $req;
mail($mail_To, $mail_Subject, $mail_Body, $mail_From);
// Create post object
$my_post = array(
'post_title' => $txnID,
'post_status' => 'publish',
'post_author' => 1,
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_type' => 'post_pledges',
);
$post_id = wp_insert_post( $my_post, true );
// Send an email announcing "after post_pledges creation"
$mail_From = "[email protected]";
$mail_To = "[email protected]";
$mail_Subject = "after post_pledges creation";
$mail_Body = $post_id;
mail($mail_To, $mail_Subject, $mail_Body, $mail_From);
add_post_meta($post_id, "wpl_pledge_cause", $donCause);
add_post_meta($post_id, "wpl_pledge_transaction_id", $txnID);
add_post_meta($post_id, "wpl_pledge_first_name", $firstName);
add_post_meta($post_id, "wpl_pledge_last_name", $lastName);
add_post_meta($post_id, "wpl_pledge_country", $addressCountry);
add_post_meta($post_id, "wpl_pledge_city", $addressCity);
add_post_meta($post_id, "wpl_pledge_address", $addressStreet);
add_post_meta($post_id, "wpl_pledge_postal_code", $addressZip);
add_post_meta($post_id, "wpl_pledge_email", $payerEmail);
add_post_meta($post_id, "wpl_pledge_donation_amount", $payment_gross);
add_post_meta($post_id, "wpl_pledge_payment_source", 'paypal');
add_post_meta($post_id, "wpl_pledge_payment_Status", $payment_status);
}
// Response is VERIFIED
// Send an email announcing the IPN message is VERIFIED
$mail_From = "[email protected]";
$mail_To = "[email protected]";
$mail_Subject = "VERIFIED IPN";
$mail_Body = $req;
mail($mail_To, $mail_Subject, $mail_Body, $mail_From);
} else if (strcmp ($res, "INVALID") == 0) {
// IPN invalid, log for manual investigation
// Notification protocol is NOT complete, begin error handling
// Send an email announcing the IPN message is INVALID
$mail_From = "[email protected]";
$mail_To = "[email protected]";
$mail_Subject = "INVALID IPN";
$mail_Body = $req;
mail($mail_To, $mail_Subject, $mail_Body, $mail_From);
}
?>
I read about a problem with an infinite loop and maybe it's also for me because the script doesn't go on when try to execute the function.
Have you any ideas?
$post_id
after you attempt to insert the post? if wp_insert_post fails, that will be an error object, you should log it to the log file and see what it contains.