私は PHP の経験がまったくないことから始めなければなりませんが、このスクリプトがそれほど野心的ではないことはわかっています。
Wordpress の metaWeblog API を使用して、数百の投稿をまとめて作成しています。各投稿には個別のタイトル、説明、および 2 つの画像の URL が必要です。後者はカスタム フィールドです。
次のファイルに手動でデータを入力して、1 つの投稿を作成することに成功しました。
<?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);
}
?>
このように動作するはずです。
- fileLoop.php はディレクトリを開き、各ファイルを反復処理します
- ディレクトリ内の各ファイルに対して、適切な投稿タイトル (postTitle) が作成され、サーバーのファイルへの URL パス (imagePath) が作成されます。
- 各 postTitle と imagePath は、metaWeblog.php の関数 createPost に渡されます。
- metaWeblog.php は投稿を作成し、投稿 ID を返して、ディレクトリ内の各ファイルのテーブル行の作成を完了します。
fileLoop.php で関数を宣言しようとしましたが、ファイルを完全に結合しようとしました。すべてのファイルを含むテーブルを作成するか、そのようにディレクトリをステップスルーしません。私は何かが欠けています、私はそれを知っています。ここに $POST_ を組み込む方法、またはセッションを使用する方法がわかりません。私は、PHP でのプログラミングに非常に慣れていないと言いました。