Problems with require('wp-load.php')

help me!
i think parsing a webpage then insert contents into wordpress wp_post table.
but, when i require(‘wp-load.php’); it throw Fatal error: Call to a member function find() on a non-object in C:\wamp\www\simple_html_dom.php on line 879.

when i disable //require(‘wp-load.php’); it throw Fatal error: Call to undefined function wp_insert_post() in C:\wamp\www\ estinclud.php on line 22

the wp_insert_post($post); can not to work, why?

<?php
//require('wp-load.php');
require('simple_html_dom.php');  

ini_set("max_execution_time",0);

 $post = array();
 $post['post_status'] = 'publish';
 $post['post_author'] = 1;
 $post['comment_status'] = 'closed';
 //$post['post_category'] = array(6);
 $post['post_date'] = date('Y-m-d H:i:s',strtotime("now"));
 $post['post_content'] = '';

$parseURL = 'http://www.tradebit.com/visit.php/71080/product/-/108804966';//this site will be parse
parse_html($parseURL);

echo '$post post_content ' . $post['post_content']  . '<br>';
echo 'title' . '<br>';
echo $post['post_title'] . '<br>';
//insert $post into wp_post table
wp_insert_post($post);
/**
parsing webpage
@param string $parseURL 
@return $post, this $post will be insert into wp_post table
*/
function parse_html($parseURL){
global $post;   
$html = new simple_html_dom();   
$html->load_file($parseURL);  

foreach($html->find('title') as $element) { 
$title = preg_split ( '/ - /', $element->plaintext);
} 
$post['post_title'] = $title[0];
$post['post_content'] .=  $html->find('div[class=orangeContent]',1);  # get orangeContent as post_content

return $post;
// tear down
$html->clear();
unset($html);
} 
?>

i require_once(‘wp-load.php’); at wp_insert_post($post) can be run once.

$row[‘FileID’] = ‘108804966’ //get $row[‘FileID’] from database,it with $parseURL Form a complete Web site

$parseURL = ‘http://www.tradebit.com/visit.php/71080/product/-/’;

//get $row[‘FileID’] from database,it with $parseURL Form a complete Web site.

like this ‘http://www.tradebit.com/visit.php/71080/product/-/108804966

while ($row = mysql_fetch_array($results, MYSQL_ASSOC)) {
global $post;
parse_html($parseURL_pre . $row[‘FileID’]); i require_once(‘wp-load.php’); at wp_insert_post($post) can be run once.
wp_insert_post($post); //insert $post into wp_post table

}

why??? after i require_once(‘wp-load.php’) the ‘simple_html_dom.php’ can not be use.

<?php
//require('wp-load.php');
require('simple_html_dom.php');  

ini_set("max_execution_time",0);

 $post = array();
 $post['post_status'] = 'publish';
 $post['post_author'] = 1;
 $post['comment_status'] = 'closed';
 //$post['post_category'] = array(6);
 $post['post_date'] = date('Y-m-d H:i:s',strtotime("now"));
 $post['post_content'] = '';

$parseURL = 'http://www.tradebit.com/visit.php/71080/product/-/108804966';//this site will be parse
parse_html($parseURL);

//insert $post into wp_post table
[B]require_once('wp-load.php'); // wp_insert_post($post) can be run once[/B]
wp_insert_post($post);
/**
parsing webpage
@param string $parseURL 
@return $post, this $post will be insert into wp_post table
*/
function parse_html($parseURL){
global $post;   
$html = new simple_html_dom();   
$html->load_file($parseURL);  

foreach($html->find('title') as $element) { 
$title = preg_split ( '/ - /', $element->plaintext);
} 
$post['post_title'] = $title[0];
$post['post_content'] .=  $html->find('div[class=orangeContent]',1);  # get orangeContent as post_content

return $post;
// tear down
$html->clear();
unset($html);
} 
?>

anyone can help me? thanks a lot.

Hmm. I just checked and I surprisingly have never mixed those two things together. Probably because I also didn’t know that there was a wp_insert_post() function since it is a lot easier and faster to just manipulate the raw tables directly and not drag in WP. The posts table schema for WP hasn’t changed in years.

If you still want to mix them and they work fine separately, then have two separate PHP scripts and have the one that loads ‘wp-load.php’ use file_get_contents() to call the other script. Basically create a loopback request to the local web server as a hacky workaround.

BTW, doing this:

foreach($html->find(‘title’) as $element) {

Is weird. There should only be one ‘title’ element in the document, so something like this:

$title = $html->find(“title”, 0);
$title = explode(" - ", $title->plaintext);
$title = $title[0];

Is more readable, IMO.

hi Thomas Hruska
thanks your reply.

Why is this ‘include(‘wp-load.php’)’ with ‘include(‘simple_html_dom.php’)’ will have a conflict???

include ‘wp-load.php’ for run function wp_insert_post($post) ,
but once include ‘wp-load.php’ ,the parse function can not to run that in ‘simple_html_dom.php’.

<?php
//include(‘wp-load.php’); //include ‘wp-load.php’ for run function wp_insert_post($post)
include(‘simple_html_dom.php’);

$post = array();
$post[‘post_status’] = ‘publish’;
$post[‘post_author’] = 1;
$post[‘comment_status’] = ‘closed’;
$post[‘post_title’] = ‘’;
$post[‘post_content’] = ‘’;
$post[‘post_date’] = date(‘Y-m-d H:i:s’,strtotime(“now”));

$parseURL = ‘http://www.tradebit.com/visit.php/71080/product/-/87837706’;
$html = new simple_html_dom();
$html->load_file($parseURL);
$title = $html->find(“title”, 0);
$title = explode(" - ", $title->plaintext);
$post[‘post_title’] = $title[0];
$post[‘post_content’] = $html->find(‘div[class=orangeContent]’,1);

echo '$post post_content ’ . $post[‘post_content’] . ‘<br>’;
echo ‘title’ . ‘<br>’;
echo $post[‘post_title’] . ‘<br>’;

wp_insert_post($post);

$html->clear();
unset($html);

?>