필자는 PHP 경험이 없다는 말로 시작해야합니다. 그러나이 스크립트는 그 야심적인 일이 될 수 없다는 것을 알고 있습니다.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);
}
?>
는 는
다음과 같은 일을 해야하는,
는 는 는- fileLoop.php 디렉토리에있는 각 파일에 대해 각 파일
- 을 통해 디렉토리와 반복을 열고, 적절한 게시물 제목 (postTitle)가 생성됩니다 그리고 서버의 파일에 대한 URL 경로 (imagePath)는 각각
- 입니다. 각 postTitle과 imagePath는 metaWeblog.php의 createPost에 전달됩니다.
- metaWeblog.php는 게시물을 만들고 게시물 ID를 전달하여 디렉토리의 각 파일에 대한 테이블 행 작성을 완료합니다.
나는 fileLoop.php에서 함수를 선언하려고 시도했지만 파일을 완전히 결합하려고 시도했습니다. 그것은 모든 파일들로 테이블을 생성하거나 그렇게 디렉토리를 밟지 않습니다. 나는 뭔가를 놓치고있다, 나는 그것을 안다. $ POST_를 여기에 통합하는 방법을 모르거나 PHP로 프로그래밍하는 데 아주 익숙하다고 말한대로 세션을 사용합니다.
아직 해결책을 찾지 못했습니다. 내가 객체 지향 접근 방식을 시도하고 여전히 오류 또는 이상한 결과가있어. 이것은 누군가가 전에 보았고 고쳐 놓은 것이어야합니다. 나는이 일을 절실히 필요로하므로 더 이상의 도움이 될 것입니다. – frankV