• Hi i make onde function when working save_post_noticias hook, but show me error like a JSON IS NOT VALID or TERMS INVALID.
    In this case the function make 2 things get words, inside te content and save inside terms, after this get url via curl and send response via curl post for external endpoint.

    add_action( 'save_post_noticia', 'terms_save', 20);
    
    function terms_save() {
        global $post;
    
        //$str = html_entity_decode($post->post_content);
        $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")]');
        //$at = $x->evaluate('//span[contains(@class, "at")]');
      
        //var_dump($hash);
        //var_dump($tickers);
        //var_dump($siglas);
        
        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 );
      
            }
          }
        }
    
        $post_id = $post->ID;
    
        viacurl($post_id);
        
    }
    
    function viacurl($post_id)
    {
    
    	$curl = curl_init();
    
    	curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://localhost/mensageria/wp-json/wp/v2/noticias/'.$post_id.'/?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');
    
      $data = $response;
      $url = $urlendpoint;
    
      httpPost($url, $data);
    
    }
    
    function httpPost($url, $data)
    {
    
        $tokenendpoint = get_field('token_endpoint_notices', 'option');
        $typetoken = get_field('field_635c140300703','option');
    
        $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 'sucess api send';
        } else { 
            echo 'api not send';
        }
    
        curl_close($curl);
    
        return $response;
    }
    • This topic was modified 2 years, 4 months ago by t-p. Reason: Moved to Fixing WordPress from Developing with WordPress
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘help i have error JSON is not valid’ is closed to new replies.