Wordpress XMLRPC returning blank, but successful
I'm trying to post to WordPress using curl via PHP - I'm posting using the XMLRPC built into Wordpress by default.
Posting is successful with the below code, but nothing is returned. I need to know some information about the post, such as it's URL - I can do this if I have the 'post ID', which by looking at the xmlrpc.php file, it should return. Below is my code for posting:
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;
}
The echo '<pre>'.print_r($re...
line just shows <pre></pre>
. I have savd the verbose output of curl into a file, please find it below (I've starred out the 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
As I said, the CODE DOES POST, BUT NOTHING IS RETURNED. Sorry to be blunt, but I know this will start off a torrent of pointless answers. So, should I be expecting a post ID to be returned, and if not, how can I easily g开发者_Go百科et it returned?
Thanks
Sod's law. After posting I tried changing the max timeout time: curl_setopt($ch, CURLOPT_TIMEOUT, 1);
to 10: curl_setopt($ch, CURLOPT_TIMEOUT, 10);
and I get some nice XML returned with the post ID embedded.
I haven't deleted this post as I thought it might be useful for someone.
It will return ($results) the results as xml ... I think in your program it wont display anything in screen ( But u can see that xml data in source code of the output screen ) .. You should use xmlrpc_decode or XML parsing function to get the data from returned XML .. In your program it will return the newly created post id.
For your program, I think folowing changes will do the work
$results = curl_exec($ch);
$results = xmlrpc_decode($results);
echo '<pre>'.print_r($results, true).'</pre>';
精彩评论