远程请求不能正常工作

问题描述:

我写一个自定义函数post_updated行动在WordPress象下面这样:远程请求不能正常工作

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:博客是存在于项目的根的子文件。这是造成这个问题吗?

+3

您是否检查过'wp_remote_post()'函数实际返回的内容?如果出现问题,它应该返回一个'WP_Error'对象,这可能会给你更多的见解。 –

+0

是的。它给我404错误。 “请求的URL/feed/blogAPI在此服务器上未找到。” – saikiran

+1

您确定URL中没有拼写错误吗?正确的外壳? –

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

但我的wordpress是在https://dev.kidengage.com/blog根目录的子文件夹中。

所以使用wp_remote_post将导致时髦的响应。

我结束了使用像下面直接从wordpress存储数组到我的根使用的另一个数据库。

 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);  

     } 
+0

downvoter请注意解释为何投下票? – saikiran