Problem with wp_insert_post

时间:2014-05-12 作者:Manuel Ragazzini

我为我的英语感到抱歉。

脚本中的wp\\u insert\\u post函数有问题。

当我收到贝宝的通知时,我正在尝试创建一个新帖子。在我尝试在数据库中创建新帖子之前,一切正常。

这是我的代码:

<?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);
}
?>
我读到了关于无限循环的一个问题,可能也是因为脚本在尝试执行函数时无法继续。

你有什么想法吗?

1 个回复
SO网友:s_ha_dum

因为你提到了一个无穷大,我猜你被一个save_post that fires for wp_insert_post() 因此,如果您将代码挂接到将运行的代码中,那么就反复使用相同的挂接。

要避免这种情况,请从回调的顶部删除该操作:

remove_action(\'save_post\',\'yourcallbackfunctionname\');
$my_post = array(
    // ...
 );
 $post_id = wp_insert_post( $my_post, true );

结束

相关推荐

PayPal Post IPN处理噩梦

我正在尝试将paypal标准与我的Wordpress网站整合,一切都进展顺利,直到我想做一件简单的事情:用户提交paypal付款,paypal将IPN发布到我的服务器。Post IPN挂钩添加一个用户元。如果用户重新加载当前页面,它会检查前一个用户元是否设置为1,如果设置为1,则会显示“您已经购买了您的票证”。我想这很简单,但如果我将此添加到我的functions.php 对于后IPN挂钩:add_filter(\"gform_paypal_post_ipn\", \"update_order_statu