【问题标题】:remote request from functions.php is not working来自functions.php的远程请求不起作用
【发布时间】:2017-12-18 14:25:27
【问题描述】:

我正在为 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

我做错了什么。

查看我的发帖人请求:

PS: 该博客位于项目根目录的子文件夹中。这是造成问题的原因吗?

【问题讨论】:

  • 您检查过wp_remote_post() 函数实际返回的内容吗?如果出现问题,它应该返回一个 WP_Error 对象,这可能会给您带来更多洞察力。
  • 是的。它给了我 404 错误。 " 在此服务器上找不到请求的 URL /feed/blogAPI。"
  • 您确定网址中没有拼写错误吗?正确的大小写?
  • 您在服务器主机文件中没有覆盖远程主机名的记录?服务器是否可以从终端从 curl 访问该 URL?
  • @MagnusEriksson 如果这是问题所在,它不会也覆盖邮递员中的远程主机名吗?

标签: php wordpress curl wordpress-hook


【解决方案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);    

        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 2022-12-15
    • 2017-07-07
    • 2022-01-14
    • 2011-09-13
    • 2013-05-22
    相关资源
    最近更新 更多