2010-12-23 75 views
3

我想通過PHP使用curl發佈到WordPress - 默認情況下,我使用內置到WordPress的XMLRPC發佈。WordPress的XMLRPC返回空白,但成功

以下代碼發送成功,但沒有返回。我需要知道關於帖子的一些信息,比如它的URL - 如果我有'帖子ID',通過查看xmlrpc.php文件,它應該返回,我可以這樣做。下面是我對郵政代碼:

function post($username, $password, $title, $content, $url, $category=array(), $keywords='', $type='Wordpress') 
{ 
    $encoding = 'UTF-8'; 

    $title = htmlentities($title,ENT_NOQUOTES,$encoding); 
    $keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding); 

    $reqparams = array(
     'title'=>$title, 
     'description'=>$content, 
     'mt_allow_comments'=>0, // 1 to allow comments 
     'mt_allow_pings'=>0, // 1 to allow trackbacks 
     'post_type'=>'post', 
     'mt_keywords'=>$keywords, 
     'categories'=>$category 
    ); 
    $params = array(0,$username,$password,$reqparams,true); 
    $request = xmlrpc_encode_request('metaWeblog.newPost',$params); 
    $ch = curl_init(); 

    $fp = fopen('/home/*/public_html/file.txt', 'w+'); 

    curl_setopt($ch, CURLOPT_POSTFIELDS, $request); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 1); 
    curl_setopt($ch, CURLOPT_VERBOSE, 1); 
    curl_setopt($ch, CURLOPT_STDERR, $fp); 
    $results = curl_exec($ch); 
    echo '<pre>'.print_r($results, true).'</pre>'; 
    curl_close($ch); 
    return $results; 

} 

echo '<pre>'.print_r($re...線只顯示<pre></pre>。我已經SAVD​​捲曲的詳細輸出到一個文件,請在下面找到它(我已經出演了URL):

* About to connect() to www.*******.com port 80 (#0) 
* Trying 87.106.55.179... * connected 
* Connected to www.*******.com (87.*.*.179) port 80 (#0) 
> POST /xmlrpc.php HTTP/1.1 
Host: www.*******.com 
Accept: */* 
Content-Length: 1445 
Content-Type: application/x-www-form-urlencoded 
Expect: 100-continue 

< HTTP/1.1 100 Continue 
* Operation timed out after 1000 milliseconds with 0 bytes received 
* Closing connection #0 

正如我所說,代碼沒有職位,但不返回任何結果。對不起,直言不諱,但我知道這將開始一個毫無意義的答案洪流。那麼,我是否應該期待一個帖子ID被退回,如果沒有,我怎樣才能輕鬆地將它退回?

謝謝

回答

4

Sod的法律。張貼後,我試着改變最大超時時間:curl_setopt($ch, CURLOPT_TIMEOUT, 1);到10:curl_setopt($ch, CURLOPT_TIMEOUT, 10);,我得到了一些很好的XML與嵌入的帖子ID返回。

我還沒有刪除這篇文章,因爲我認爲這可能對某人有用。

-2

它將返回($結果)的結果作爲XML ......我想在你的程序,它不會顯示在屏幕上的任何(但你可以看到在輸出畫面的源代碼,XML數據)..你應該使用xmlrpc_decode或XML解析函數從返回的XML中獲取數據。在您的程序中,它將返回新創建的帖子ID。

你的程序,我認爲如下因素的變化將做的工作

$results = curl_exec($ch); 
$results = xmlrpc_decode($results); 
echo '<pre>'.print_r($results, true).'</pre>'; 
+2

回答很久以前,似乎是一個毫無意義的答案只是發佈一個更無意義的鏈接。 -1 – Mike 2011-08-01 10:18:45