• Boa Tarde

    É no caso eu ja verifiquei os plugins, e não são.
    É estou usando o hook save_post, ele faz o seguinte, ele reconhece alguns termos que estão entre span com classes especificas e usando o insert_term eu crio um termo ja inderindo ele dentro do post.
    Isso funciona corretamente, mas ai vem a questão:
    No caso eu preciso que toda vez que eu salvar um post, pode ser no save_post ou o after_insert_post que ele ativa após a inserção no banco; eu preciso enviar uma requisição get e post para um endpoint externo.
    Ai que vem a questão, eu ja fiz isso via CURL, mas não sei se é a melhor maneira de fazer isso e ele esta funcionando esta enviando o JSON, mas ainda sim ele me retorna o erro JSON INVÁLIDO.
    Poderiam me ajudar?

    add_action('save_post','save_post_callback');
    add_action('wp_after_insert_post', 'send_post');
    
    function save_post_callback($post_id){
        global $post; 
    
        if ($post->post_type != 'noticia'){
            return;
        }
        else{
    
          $str = !empty(html_entity_decode($post->post_content)) ? html_entity_decode($post->post_content) : 'null';
          $d = new DOMDocument();
          $d->loadHTML( $str );
          $x = new DOMXpath($d);
          $hash = $x->evaluate('//span[contains(@class, "hash")]');
          $tickers = $x->evaluate('//span[contains(@class, "dollar")]');
          $siglas = $x->evaluate('//span[contains(@class, "tag")]');
          
          if( count( $hash ) ){
            $array = array();
            $int = count( $hash );
            foreach ( $hash as $el ) {
              $term_name = str_replace('#', '', $el->nodeValue );
        
              for ($i=0; $i < $int; $i++) { 
                array_push($array, $term_name);
              }
        
              if(!term_exists( $term_name, 'tags_notice')){
        
                wp_insert_term($term_name, 'tags_notice');
                wp_set_object_terms( $post->ID, $array, 'tags_notice', false );
        
              }else{
                wp_set_object_terms( $post->ID, $array, 'tags_notice', false );
              }
    
              
            }
          }
        
          if( count( $tickers ) ){
            $array = array();
            $int = count( $tickers );
            foreach ( $tickers as $ticker ) {
              $term_name = str_replace('$', '', $ticker->nodeValue );
        
              for ($i=0; $i < $int; $i++) { 
                array_push($array, $term_name);
              }
        
              if(!term_exists( $term_name, 'ativos_notice')){
        
                wp_insert_term($term_name, 'ativos_notice');
                wp_set_object_terms( $post->ID, $array, 'ativos_notice', false );
                
        
              }else{
        
                wp_set_object_terms( $post->ID, $array, 'ativos_notice', false );
        
              }
        
            }
          }
        
          if(  count( $siglas ) ){
            $array = array();
            $int = count( $siglas );
            foreach ( $siglas as $sigla ) {
              
              for ($i=0; $i < $int; $i++) { 
                array_push($array, $sigla->nodeValue);
              }
        
              if(!term_exists( $sigla->nodeValue, 'siglas_notice')){
        
                wp_insert_term($sigla->nodeValue, 'siglas_notice');
                wp_set_object_terms( $post->ID, $array, 'siglas_notice', false );
        
              }else{
        
                wp_set_object_terms( $post->ID, $array, 'siglas_notice', false );
        
              }
            }
          }
    
        }
    }
    
    $id_notice = $_GET['post']; //is working
    
    function send_post($id_notice){
          $curl = curl_init();
    
          curl_setopt_array($curl, array(
                CURLOPT_URL => 'http://localhost/mensageria/wp-json/wp/v2/noticias/'.$id_notice.'/?acf_format=standard',
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'GET',
          ));
    
          $response = curl_exec($curl);
    
          curl_close($curl);
    
          $urlendpoint = get_field('endpoint_url_notices', 'option'); //toeknkeycloack
    
          $data = $response;
          $url =  'http://localhost:8081/NewPost?'; //$urlendpoint;
    
          httpPost($url, $data);
    }
    
    function httpPost($url, $data)
    {
    
        $tokenendpoint = '****';
        $typetoken = 'Content-Type: application/json';
    
        $curl = curl_init();
    
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POSTFIELDS => $data,
            CURLOPT_HTTPHEADER => array($tokenendpoint, $typetoken),
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_RETURNTRANSFER => true,
        ));
    
        $response = curl_exec($curl);
    
        // Further processing ...
        if ($response == "OK") { 
          echo '<div class="notice notice-success is-dismissible">
                  <p>Conteudo enviado!</p>
                </div> ';
        } else { 
          echo '<div class="notice notice-error is-dismissible">
                  <p>Conteudo não Enviado</p>
                </div>';
        }
    
        curl_close($curl);
    
        return $response;
    }

    A página que eu preciso de ajuda: [fazer login para ver o link]

  • O tópico ‘JSON Inválido’ está fechado para novas respostas.