2011-11-25 2 views
2

필자는 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); 
} 

?> 
는 는

다음과 같은 일을 해야하는,

는 는 는
  1. fileLoop.php 디렉토리에있는 각 파일에 대해 각 파일
  2. 을 통해 디렉토리와 반복을 열고, 적절한 게시물 제목 (postTitle)가 생성됩니다 그리고 서버의 파일에 대한 URL 경로 (imagePath)는 각각
  3. 입니다. 각 postTitle과 imagePath는 metaWeblog.php의 createPost에 전달됩니다.
  4. metaWeblog.php는 게시물을 만들고 게시물 ID를 전달하여 디렉토리의 각 파일에 대한 테이블 행 작성을 완료합니다.

나는 fileLoop.php에서 함수를 선언하려고 시도했지만 파일을 완전히 결합하려고 시도했습니다. 그것은 모든 파일들로 테이블을 생성하거나 그렇게 디렉토리를 밟지 않습니다. 나는 뭔가를 놓치고있다, 나는 그것을 안다. $ POST_를 여기에 통합하는 방법을 모르거나 PHP로 프로그래밍하는 데 아주 익숙하다고 말한대로 세션을 사용합니다.

+0

아직 해결책을 찾지 못했습니다. 내가 객체 지향 접근 방식을 시도하고 여전히 오류 또는 이상한 결과가있어. 이것은 누군가가 전에 보았고 고쳐 놓은 것이어야합니다. 나는이 일을 절실히 필요로하므로 더 이상의 도움이 될 것입니다. – frankV

답변

0

createPost() 함수의 선언을 업데이트하여 보내려는 매개 변수를 고려해야합니다. the associated manual page에서 찾을 수 있습니다 PHP 함수 인수에 대한

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

    ... 

} 

더 많은 정보 :

는 그래서 이런 걸해야한다.

일단이 문제가 해결되면 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에서 요청을 거부하는 경우 표시됩니다.

+0

빠른 답장을 보내 주셔서 감사합니다! 이것을 시도했다. 출력 테이블을 올 Y로 생성하지만 포스트를 작성하지 않으며 포스트 ID를 리턴하지 않습니다. – frankV

+0

내 대답이 더 많은 정보로 업데이트되었습니다. – Treffynnon

+0

Get this'경고 : curl_setopt()는 curl_setopt()뿐만 아니라 매개 변수 1이 resource, null 일 때 'null'을 기대합니다. 나는 내가 수색으로 무엇을 찾을 수 있는지 보게 될 것이다. – frankV

관련 문제