2

I should start by saying I have no php experience what so ever, but I know this script can't be that ambitious.

I'm using Wordpress' metaWeblog API to batch the creation of several hundred posts. Each post needs a discrete title, a description, and url's for two images, the latter being custom fields.

I have been successful producing one post by manually entering data into the following file;

<?php    // metaWeblog.Post.php
$BLOGURL = "http://path/to/your/wordpress";
$USERNAME = "username";
$PASSWORD = "password";

function get_response($URL, $context) {
if(!function_exists('curl_init')) {
die ("Curl PHP package not installed\n");
}

/*Initializing CURL*/
$curlHandle = curl_init();

/*The URL to be downloaded is set*/
curl_setopt($curlHandle, CURLOPT_URL, $URL);
curl_setopt($curlHandle, CURLOPT_HEADER, false);
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $context);

/*Now execute the CURL, download the URL specified*/
$response = curl_exec($curlHandle);
return $response;
}

function createPost(){
/*The contents of your post*/
$description = "post description";

/*Forming the content of blog post*/
$content['title'] = $postTitle;
$content['description'] = $description;
/*Pass custom fields*/
   $content['custom_fields'] = array(
array( 'key' => 'port_thumb_image_url', 'value' => "$imagePath" ),
array( 'key' => 'port_large_image_url', 'value' => "$imagePath" )
);
/*Whether the post has to be published*/
$toPublish = false;//false means post will be draft
$request = xmlrpc_encode_request("metaWeblog.newPost",
array(1,$USERNAME, $PASSWORD, $content, $toPublish));

/*Making the request to wordpress XMLRPC of your blog*/
$xmlresponse = get_response($BLOGURL."/xmlrpc.php", $request);
$postID = xmlrpc_decode($xmlresponse);
echo $postID;
}
?>

In an attempt to keep this short, here is the most basic example of the script that iterates through a directory and is "supposed" to pass the variables $postTitle, and $imagePath and create the posts.

<?php     // fileLoop.php
require('path/to/metaWeblog.Post.php');

$folder = 'foldername';
$urlBase = "images/portfolio/$folder";//truncate path to images

if ($handle = opendir("path/to/local/images/portfolio/$folder/")) {

/*Loop through files in truncated directory*/
while (false !== ($file = readdir($handle))) {
    $info = pathinfo($file);
    $file_name =  basename($file,'.'.$info['extension']);   // strip file extension

    $postTitle = preg_replace("/\.0|\./", " ", $file_name); // Make file name suitable for post title !LEAVE!
        echo "<tr><td>$postTitle</td>";

    $imagePath = "$urlBase/$file";
        echo " <td>$urlBase/$file</td>";

    createPost($postTitle, $imagePath);

    }

closedir($handle);
}

?>

It's supposed to work like this,

  1. fileLoop.php opens the directory and iterates through each file
  2. for each file in the directory, a suitable post title(postTitle) is created and a url path(imagePath) to the server's file is made
  3. each postTitle and imagePath is passed to the function createPost in metaWeblog.php
  4. metaWeblog.php creates the post and passes back the post id to finish creating the table row for each file in the directory.

I've tried declaring the function in fileLoop.php, I've tried combining the files completely. It either creates the table with all files, or doesn't step through the directory that way. I'm missing something, I know it. I don't know how to incorporate $POST_ here, or use sessions as I said I'm very new to programming in php.

frankV
  • 5,353
  • 8
  • 33
  • 46
  • I still haven't found a solution to this. I've tried an object oriented approach and still got errors or weird results. This has to be something someone has seen and fixed before. Any further help would be wonderful as I'm desperate to get this working. – frankV Dec 14 '11 at 05:21

1 Answers1

0

You need to update your declaration of the createPost() function so that it takes into account the parameters you are attempting to send it.

So it should be something like this:

function createPost($postTitle, $imagePath){
    /*The contents of your post*/
    $description = "post description";

    ...

}

More information about PHP function arguments can be found on the associated manual page.

Once this has been remedied you can use CURL debugging to get more information about your external request. To get more information about a CURL request try setting the following options:

/*Initializing CURL*/
$curlHandle = curl_init();

/*The URL to be downloaded is set*/
curl_setopt($curlHandle, CURLOPT_URL, $URL);
curl_setopt($curlHandle, CURLOPT_HEADER, false);
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $context);


curl_setopt($curlHandle, CURLOPT_HEADER, true); // Display headers
curl_setopt($curlHandle, CURLOPT_VERBOSE, true); // Display communication with server

/*Now execute the CURL, download the URL specified*/
$response = curl_exec($curlHandle);

print "<pre>\n";
print_r(curl_getinfo($ch));  // get error info
echo "\n\ncURL error number:" .curl_errno($ch); // print error info
echo "\n\ncURL error:" . curl_error($ch); 
print "</pre>\n";

The above debug example code is from eBay's help pages.

It should show you if Wordpress is rejecting the request.

Treffynnon
  • 21,365
  • 6
  • 65
  • 98
  • Thanks for the quick reply! Tried this. Produces the output table fine but does not create the posts and it does not return the post ID. – frankV Nov 25 '11 at 18:08
  • Get this `Warning: curl_setopt() expects parameter 1 to be resource, null given' a bunch, not just curl_setopt(). I'll see what I can find out with a search. – frankV Nov 25 '11 at 19:20
  • Have updated code. I had the curl handle variable incorrectly named. Try again. – Treffynnon Nov 25 '11 at 23:15
  • Ok, noticed some syntactical errors, now that they're corrected, I see this `* malformed * malformed * malformed * `... – frankV Nov 26 '11 at 03:26
  • Looks like Wordpress is rejecting your request. I know nothing about XML RPC so you will need to check the documentation for that part or ask another question relating to only that part of the project. – Treffynnon Nov 26 '11 at 14:28