来自functions.php的远程请求无效

时间:2017-12-18 14:25:28

标签: php wordpress curl wordpress-hook

我正在为wordpress中的 post_updated 操作编写自定义函数,如下所示:

function post_update_trigger($post_ID, $post_after, $post_before){

    if($post_after->post_status == "publish" || $post_after->post_status == "trash" ){

        $url="https://myremoteurl.com/feed/blogAPI";

       $response = wp_remote_post($url,array(
                         'method' => 'POST',
                         'timeout' => 45,
                         'redirection' => 5,
                         'httpversion' => '1.0',
                         'blocking' => true,
                         'headers' => array(),
                         'body' => $postFields,
                         'cookies' => array()
                       ));

           if ( is_wp_error( $response ) ) {
                    $error_message = $response->get_error_message();
                    echo "Something went wrong: $error_message";exit;
            } else {
                    echo 'Response:<pre>';
                   print_r( $response );exit;
                   echo '</pre>';
    }

    }

}

add_action( 'post_updated', 'post_update_trigger', 10, 3 );

我尝试了邮递员的邮寄请求。一切似乎很好,工作。除了 wp_remote_post 之外,我也尝试了 CURL

我做错了什么。

看我的帖子人请求:

postman request image

PS:博客位于项目根目录的子文件夹中。这会导致问题吗?

1 个答案:

答案 0 :(得分:-1)

错误是由于请求与远程相同的原点,即我的控制器位于https://dev.kidengage.com/feed/blogAPI

但我的wordpress位于根目录的子文件夹中,位于https://dev.kidengage.com/blog

所以使用 wp_remote_post 会产生时髦的回应。

我最终使用如下所示将数组直接从wordpress存储到我的root使用的另一个数据库。

        global $wpdb;

        $newdb = new wpdb('username','password','dagtabasename','localhost');
        $newdb->show_errors();

        $postExist= $newdb->get_row("select * from blogMaster where postID=$post_ID");

        if(empty($postExist)){

            $insert=array(
                "postID" => $post_ID,
                "postTitle" => $post_after->post_title,
                "postContent" => $post_after->post_content ,
                "postExcerpt" =>  $post_after->post_excerpt,
                "postType" =>  $post_after->post_type,
                "postGuid" => $post_after->guid ,
                "postPermLink" =>  $permLink,
                "postThumb" =>  $thumbLarge,
                "postAuthorName" => $authorName,
                "postAuthorPic" => $authorURL ,
                "postStatus" => $post_after->post_status,
                "postDate" =>  $post_after->post_date,
                "postModified" =>  $post_after->post_modified
            );


            $newdb->insert("blogMaster",$insert);    

        }