首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为目录中的每个文件传递两个参数的PHP运行函数

为目录中的每个文件传递两个参数的PHP运行函数
EN

Stack Overflow用户
提问于 2011-11-25 17:35:11
回答 1查看 320关注 0票数 2

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

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

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

代码语言:javascript
复制
<?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并创建帖子的脚本的最基本示例。

代码语言:javascript
复制
<?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. 对于目录中的每个文件,将创建一个合适的post标题(PostTitle),并为服务器的文件创建一个url路径(ImagePath)
  3. 每个postTitle和imagePath都被传递给metaWeblog.php中的函数createPost
  4. metaWeblog.php创建post并传回post id,以完成为目录中的每个文件创建表行。

我尝试在fileLoop.php中声明函数,我尝试将文件完全组合起来。它要么用所有文件创建表,要么不以这种方式遍历目录。我漏掉了什么,我知道。我不知道如何在这里合并$POST_,也不知道如何使用会话,因为我说过我对用php编程非常陌生。

EN

回答 1

Stack Overflow用户

发布于 2011-11-25 18:02:01

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

所以应该是这样的:

代码语言:javascript
复制
function createPost($postTitle, $imagePath){
    /*The contents of your post*/
    $description = "post description";

    ...

}

有关information参数的更多信息可以在相关联的手册页上找到。

一旦纠正了这一点,您就可以使用CURL调试来获取有关外部请求的更多信息。要获得有关CURL请求的更多信息,请尝试设置以下选项:

代码语言:javascript
复制
/*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";

以上调试示例代码来自易趣的帮助页面

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8272498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档