Ondersteuning » Code oplossingen gezocht » PHP code op pagina uitvoeren

  • Opgelost ProCessor

    (@processor)


    Hallo,

    Voor een autobedrijf moet ik PHP code integreren om advertenties (via een portal) op te halen.

    Nou wil ik dit op een specifieke pagina van de site weergeven.

    De code die hiervoor nodig is bevat 2 bestanden:
    index.php

    <?php
        error_reporting(E_ALL); ini_set("display_errors", "on");
        include_once("xml.class.php");
    
        //hier post informatie
        //$xmlData =  file_get_contents("php://input");
    
        //zo ziet de output eruit.
        $xmlData =  file_get_contents("input/xmlinput-1342704070.xml");
    
        $fh = fopen("input/xmlinput-" . time() . ".xml", "w");
        fwrite($fh, $xmlData);
        fclose($fh);
    
        $xml = new vweXML();
        $xml->setVar("XMLinput", $xmlData);
        $result = $xml->processXMLcontent();
    
        //$result = $xml->getResult();
    
        $result = $xml->getVar("XMLoutput");
    
        echo '<pre>';
    
        var_dump($result);
    ?>

    En xml.class.php

    <?php
       class vweXML
       {
          /**
           * XML input
           */
          protected $XMLinput;
    
          /**
           * XML output
           */
          protected $XMLoutput;
    
          /**
           * XMLReader object
           */
          protected $doc;
    
          /**
           * Last error message
           */
          protected $error;
    
          /**
           * Proces result
           */
          protected $result;
    
          /**
           * Process the XML content
           *
           * @return  void
           */
          public function processXMLcontent( )
          {
             if ( isset( $this->XMLinput ) )
             {
                if ( ! isset( $this->XMLoutput ) )
                {
                    $this->doc = new XMLReader( );
    
                    //Parse XML document
                    if ( $this->doc->XML( stripslashes( $this->XMLinput ) ) )
                    {
                       //Convert the document to an array
                       if ( $this->XMLoutput = $this->xml2assoc( ) )
                       {
                          $this->doc->close( );
    
                          return true;
                       }
                    }
    
                    $this->doc->close( );
    
                    $this->setResult( "Incorrect XML format" );
    
                    return false;
                }
                else
                {
                    return $this->XMLoutput;
                }
             }
             else
             {
                $this->setResult( "No XML input was found" );
             }
             return false;
          }
    
          /**
           * XML to associative array
           *
           * @return  void
           */
          protected function xml2assoc( )
          {
             $tree = null;
             while ( $this->doc->read( ) )
             {
                switch ( $this->doc->nodeType )
                {
                   case XMLReader::END_ELEMENT: return $tree;
    
                   case XMLReader::ELEMENT:
    
                      $node = array(
                                  'tag' => $this->doc->name,
                                  'value' => $this->doc->isEmptyElement ? '' : $this->xml2assoc( $this->doc )
                               );
    
                      if ( $this->doc->hasAttributes )
                      {
                         while( $this->doc->moveToNextAttribute( ) )
                         {
                            $node['attributes'][ $this->doc->name ] = $this->doc->value;
                         }
                      }
                      $tree[] = $node;
    
                      break;
                   case XMLReader::TEXT:
                   case XMLReader::CDATA:
                      $tree .= $this->doc->value;
                }
             }
    
             return $tree;
          }
    
          /**
           * Retrieves a var from the object
           *
           * @param      string $var
           * @return     mixed $var
           */
          public function getVar( $var )
          {
             if ( isset( $this->$var ) )
             {
                return $this->$var;
             }
    
             return false;
          }
    
          /**
           * Sets a var in the object
           *
           * @param      string $var
           * @param      mixed $value
           * @return     bool
           */
          public function setVar( $var, $value )
          {
             if ( property_exists( $this, $var ) )
             {
                $this->$var = $value;
    
                return true;
             }
    
             return false;
          }
    
          /**
           * Sets result message
           *
           * @param      string $message
           */
          protected function setResult( $message )
          {
             if ( $message )
                $this->result = $message;
          }
    
          /**
           * Returns result
           */
          public function getResult( )
          {
             if ( isset( $this->result ) )
                return $this->result;
    
             return false;
          }
    
       }
    
    ?>

    Enig idee hoe dit op een pagina te implementeren ?

4 reacties aan het bekijken - 1 tot 4 (van in totaal 4)
  • Moderator nielsvanrenselaar

    (@nielsvanrenselaar)

    Aparte pagina maken met een template tag (zie WordPress templates) en daar je PHP code op implementeren.

    Thread starter ProCessor

    (@processor)

    Zou dit dan zo moeten werken?
    page-template-vwe.php:

    <?php
    
    /*
    
    Template Name: VWE import page
    
    */
    
    ?>
    
    <?php get_header(); include get_template_directory()."/includes/tt_meta.php"; ?>
    
    			<?php  global $this_post_id; $this_post_id = $post->ID;top_widget_print('in'); 
    
    if ( theme_get_meta_option($post->ID, 'theme_show_home_widget') && !theme_get_meta_option($post->ID, 'theme_show_home_widget_pos')){home_page_widget($size);}
    
     ?>
    
    <?php
        error_reporting(E_ALL); ini_set("display_errors", "on");
        include_once("xml.class.php");
    
        //hier post informatie
        //$xmlData =  file_get_contents("php://input");
    
        //zo ziet de output eruit.
        $xmlData =  file_get_contents("input/xmlinput-1342704070.xml");
    
        $fh = fopen("input/xmlinput-" . time() . ".xml", "w");
        fwrite($fh, $xmlData);
        fclose($fh);
    
        $xml = new vweXML();
        $xml->setVar("XMLinput", $xmlData);
        $result = $xml->processXMLcontent();
    
        //$result = $xml->getResult();
    
        $result = $xml->getVar("XMLoutput");
    
        echo '<pre>';
    
        var_dump($result);
    ?>
    
    <?php if ( theme_get_meta_option($this_post_id, 'theme_show_sb_bot') && !theme_get_meta_option($this_post_id, 'theme_show_sb_bot_wide') ) {get_sidebar('bottom');} ?>
    
    <?php get_footer(); ?>

    En dan de xml.class.php in dezelfde directory.
    Volgens mij moet ik nog aangeven dat het phpscript de content van de pagina is…

    Thread starter ProCessor

    (@processor)

    Hmm… krijg het nog niet echt werkend.

    Thread starter ProCessor

    (@processor)

    Ik heb nu dit:

    <?php
    /*
    Template Name: VWE import page
    */
    ?>
    <?php get_header();?>
    
    			<?php 
    
    			if (have_posts()) {
    
    				/* Start the Loop */
    
    				while (have_posts()) {
    
    					the_post();
    
    					get_template_part('content', 'page');
    
        error_reporting(E_ALL); ini_set("display_errors", "on");
        include_once("xml.class.php");
    
        //hier post informatie
        //$xmlData =  file_get_contents("php://input");
    
        //zo ziet de output eruit.
        $xmlData =  file_get_contents("input/xmlinput-1342704070.xml");
    
        $fh = fopen("input/xmlinput-" . time() . ".xml", "w");
        fwrite($fh, $xmlData);
        fclose($fh);
    
        $xml = new vweXML();
        $xml->setVar("XMLinput", $xmlData);
        $result = $xml->processXMLcontent();
    
        //$result = $xml->getResult();
    
        $result = $xml->getVar("XMLoutput");
    
        echo '<pre>';
    
        var_dump($result);
    
    					/* Display comments */
    
    					if (theme_get_option('theme_allow_comments')) {
    
    						comments_template();
    
    					}
    
    				}
    
    			} else {
    				theme_404_content();
    			}
    			?>
    			<?php get_sidebar('bottom'); ?>
    <?php get_footer(); ?>

    Nu krijg ik de pagina goed te zien. Alleen geeft hij dit weer:
    Warning: file_get_contents(input/xmlinput-1342704070.xml): failed to open stream: No such file or directory in …/wp-content/themes/autohuisthemegrey/page-template-vwe.php on line 29 Warning: fopen(input/xmlinput-1361360305.xml): failed to open stream: No such file or directory in …/wp-content/themes/autohuisthemegrey/page-template-vwe.php on line 33 Warning: fwrite() expects parameter 1 to be resource, boolean given in …/wp-content/themes/autohuisthemegrey/page-template-vwe.php on line 34 Warning: fclose() expects parameter 1 to be resource, boolean given in …/wp-content/themes/autohuisthemegrey/page-template-vwe.php on line 35 Warning: XMLReader::XML(): Empty string supplied as input in …/wp-content/themes/autohuisthemegrey/xml.class.php on line 43
    bool(false)

    Het mapje input staat wel in dezelfde directory…

4 reacties aan het bekijken - 1 tot 4 (van in totaal 4)
  • Het onderwerp ‘PHP code op pagina uitvoeren’ is gesloten voor nieuwe reacties.