2

私は 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);
}

?>

このように動作するはずです。

  1. fileLoop.php はディレクトリを開き、各ファイルを反復処理します
  2. ディレクトリ内の各ファイルに対して、適切な投稿タイトル (postTitle) が作成され、サーバーのファイルへの URL パス (imagePath) が作成されます。
  3. 各 postTitle と imagePath は、metaWeblog.php の関数 createPost に渡されます。
  4. metaWeblog.php は投稿を作成し、投稿 ID を返して、ディレクトリ内の各ファイルのテーブル行の作成を完了します。

fileLoop.php で関数を宣言しようとしましたが、ファイルを完全に結合しようとしました。すべてのファイルを含むテーブルを作成するか、そのようにディレクトリをステップスルーしません。私は何かが欠けています、私はそれを知っています。ここに $POST_ を組み込む方法、またはセッションを使用する方法がわかりません。私は、PHP でのプログラミングに非常に慣れていないと言いました。

4

1 に答える 1

0

createPost()送信しようとしているパラメータが考慮されるように、関数の宣言を更新する必要があります。

したがって、次のようになります。

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

    ...

}

PHP 関数の引数の詳細については、関連するマニュアル ページを参照してください。

これが修正されたら、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 のヘルプ ページからのものです。

Wordpress がリクエストを拒否しているかどうかが表示されます。

于 2011-11-25T18:02:01.333 に答える