-
-
Curl HTTP 417 – Expectation Failed
July 29, 2009 -
WordPress XML RPC – HTTP/1.1 417 Expectation Failed
I was nocking up a client script to drag down a news-feed and post it to a WordPress blog; the script ran fine, no posts appeared and no errors occurred… irritating.
So I figured I’d share this pain in the arse issue fix to the “HTTP/1.1 417 Expectation Failed Error”
HTTP/1.1 417 Expectation Failed$title="Test"; $body="<b>Test</b>"; $rpcurl="http://domain.tld/xmlrpc.php"; $username=""; $password=""; $category="test"; $keywords="test"; $encoding='UTF-8'; $title = htmlentities($title,ENT_NOQUOTES,$encoding); $keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding); $content = array( 'title'=>$title, 'description'=>$body, 'mt_allow_comments'=>0, // 1 pour autoriser les commentaires 'mt_allow_pings'=>0, // 1 pour autoriser les trackbacks 'post_type'=>'post', 'mt_keywords'=>$keywords, 'categories'=>array($category) ); $params = array(0,$username,$password,$content,true); $request = xmlrpc_encode_request('metaWeblog.newPost',$params); $ch = curl_init(); curl_setopt($ch, CURLOPT_VERBOSE, '1'); // DEBUG OUTPUT - Turn this off for your live script (comment out) curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); //Fixes the HTTP/1.1 417 Expectation Failed Bug curl_setopt($ch, CURLOPT_POSTFIELDS, $request); curl_setopt($ch, CURLOPT_URL, $rpcurl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, false); $results = curl_exec($ch); curl_close($ch); echo $results;
Or as a function
function wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$category,$keywords='',$encoding='UTF-8') { $title = htmlentities($title,ENT_NOQUOTES,$encoding); $keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding); $content = array( 'title'=>$title, 'description'=>$body, 'mt_allow_comments'=>0, // 1 to allow comments 'mt_allow_pings'=>0, // 1 to allow trackbacks 'post_type'=>'post', 'mt_keywords'=>$keywords, 'categories'=>array($category) ); $params = array(0,$username,$password,$content,true); $request = xmlrpc_encode_request('metaWeblog.newPost',$params); $ch = curl_init(); curl_setopt($ch, CURLOPT_POSTFIELDS, $request); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); //Fixes the HTTP/1.1 417 Expectation Failed Bug curl_setopt($ch, CURLOPT_URL, $rpcurl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 1); $results = curl_exec($ch); curl_close($ch); return $results; }
Just to expand, checkupdown defines the error as follows
Your Web server thinks that the HTTP data stream sent by the client (e.g. CURL) contains a ‘Expect’ request which can not be satisfied. The Expect request is fairly generic i.e. loosely defined in the HTTP protocol. It can specify more than one expectation, each of which may be interpreted differently by different Web servers.
CUD: http://bit.ly/Error417
Hopefully that will save someone banging their frickin skull on the wall
Chris
http://bit.ly/Original-Source-of-Funtion-Prior-to-Fix
-


18 Responses to “ Curl HTTP 417 – Expectation Failed ”
I rarely comment on blogs but yours I had to stop and say Great Blog!!
Using the IXR PHP XML-RPC library would make this easier. It takes care of the transport level items (that you are using curl for) and allows you to focus on calling methods and processing the results.
The IXR library ships with WordPress and is found at wp-includes/class-IXR.php.
@JS Aye good point, but the blog I was targeting was actually on WordPress.com rather then hosted. Usual case of Marketing v IT for getting things done; sometimes its quicker to go else where ;o)
Thanks
Woo, you just saved my skull. Cheers!
oh men! this is great.. thanks for this line curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
this line saves me!!
Wow thanks man, im one of those whom you saved a nasty headcache
hey...
earlier I was able to work it properly but after changed my hosting it 's not working..
it's showing faultCode 403 faultString Bad login/pass combination.
there is all detail is correct I dont' know wht's prob there
Can you help me out??
@zappak: Try changing your password in what ever it is your trying to communicate with then remember to change iit in your script; if its not that then something went wrong with the service authentication process (403 faultString Bad login/pass combination.). Try changing the credentials or contacting your hosting support (as they should be able to test the same thing).
You saved my day! thanks a lot
Argh!! HTTP error 417 I was searching since Y.
Thanks AGAIN for this line curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
Thanks thanks thanks!!!
Sorry to dig up an old post but... thank fook!!!! I was literally about to rewrite my code. Thank you.
Now if only I could figure out why this just started happening and wasn't happening all along!!!
Great! Thx a lot for curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
@recent commenters
why reinvent the wheel? this functionality is already built in WP
Awesome! That fixed my problem
Thanks!!
Thank you thank you thank you!! I've been banging my head on this one for TWO weeks!!! Great post.
Super stuff. Spent or I must say wasted one day trying to find a solution for this till I hit this page. Thanks a ton.
- Gyanesh
Awesome fix! That "CURLOPT_HTTPHEADER, array('Expect:'))" line saved me hours of re-writing workaround! Used it to post a form on another non-wp site.