【问题标题】:PHP - Run function for each file in a directory passing two parametersPHP - 为传递两个参数的目录中的每个文件运行函数
【发布时间】:2012-01-06 12:49:08
【问题描述】:

首先我应该说我从来没有 php 经验,但我知道这个脚本不可能那么雄心勃勃。

我正在使用 Wordpress 的 metaWeblog API 来批量创建数百个帖子。每个帖子都需要一个独立的标题、描述和两个图像的 url,后者是自定义字段。

我已经通过手动将数据输入到以下文件中成功地发布了一篇文章;

<?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;
}
?>

为了保持简短,这里是一个最基本的脚本示例,它遍历目录并“假定”传递变量 $postTitle 和 $imagePath 并创建帖子。

<?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);
}

?>

它应该像这样工作,

  1. fileLoop.php 打开目录并遍历每个文件
  2. 为目录中的每个文件创建一个合适的帖子标题(postTitle),并创建一个指向服务器文件的 url 路径(imagePath)
  3. 每个 postTitle 和 imagePath 都被传递给 metaWeblog.php 中的 createPost 函数
  4. metaWeblog.php 创建帖子并传回帖子 id 以完成为目录中的每个文件创建表行。

我尝试在 fileLoop.php 中声明函数,我尝试完全合并文件。它要么创建包含所有文件的表,要么不以这种方式遍历目录。我错过了一些东西,我知道。 我不知道如何在此处合并 $POST_,或者使用会话,因为我说我对 php 编程非常陌生。

【问题讨论】:

  • 我还没有找到解决方案。我尝试了面向对象的方法,但仍然出现错误或奇怪的结果。这必须是某人以前见过并修复过的东西。任何进一步的帮助都会很棒,因为我迫切希望让这项工作发挥作用。

标签: php wordpress curl metaweblog


【解决方案1】:

您需要更新createPost() 函数的声明,以便它考虑到您尝试发送它的参数。

所以应该是这样的:

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

    ...

}

有关 PHP 函数参数的更多信息,请访问 the associated manual page

解决此问题后,您可以使用 CURL 调试来获取有关您的外部请求的更多信息。要获取有关 CURL 请求的更多信息,请尝试设置以下选项:

/*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";

以上调试示例代码来自eBay's help pages

它应该会显示 Wordpress 是否拒绝请求。

【讨论】:

  • 感谢您的快速回复!试过这个。可以正常生成输出表,但不会创建帖子,也不会返回帖子 ID。
  • 获取此“警告:curl_setopt() 期望参数 1 是资源,给定 null”一堆,而不仅仅是 curl_setopt()。我会看看我能通过搜索找到什么。
  • 已更新代码。我错误地命名了 curl 句柄变量。再试一次。
  • 好的,注意到一些语法错误,现在已经更正了,我看到了* malformed * malformed * malformed * ...
  • 看起来 Wordpress 拒绝了您的请求。我对 XML RPC 一无所知,因此您需要检查该部分的文档或询问仅与项目的该部分相关的另一个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-22
  • 2023-04-07
  • 1970-01-01
  • 2021-12-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多