PHP - Run function for each file in a directory passing two parameters

334 views Asked by At

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.

1

There are 1 answers

5
Treffynnon On

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.