cURL Error 26: Failed to open/read local data from file/application

Last night I started working on Lilly's 365 and very quickly ran into a few problems. Most were simple quirks of site5 fixed by getting the permissions right. The thing that kept bugging me was Phlickr, and more specifically its use of cURL.

Phlickr is an open source PHP5 based API kit used to access the Flickr API. It uses the REST method which requires that some actions are POSTs. The cURL functions in PHP can happily handle this but for some reason they were failing every time... "Failed to open/read local data from file/application" (Error 26). As soon as I disabled the CURLOPT_POST (making a GET request) cURL made no errors, though the Flickr API didn't like it.

After much searching later I found the solution was very simple... If you're not submitting any post fields then set CURLOPT_POSTFIELDS to blank, like this...

  1. $ch = curl_init($url);
  2. // make sure we submit this as a post
  3. curl_setopt($ch, CURLOPT_POST, true);
  4. if (isset($postParams)) {
  5.     curl_setopt($ch, CURLOPT_POSTFIELDS, $postParams);
  6. }
  7. else {
  8.     curl_setopt($ch, CURLOPT_POSTFIELDS, '');
  9. }

I can see that this makes sense; a POST is used for submitting data to a server so you need to submit something, even if it's blank. It would be nice if the error message was a bit more useful or better documented.

Comments

  1. Niv Niv wrote:

    Thanks, you just saved me some serious head banging.

    Mon 8th September, 2008

  2. Ed Ed wrote:

    Thank you!
    I posted a bug report (and your fix) to Phlickr's sourceforge and emailed the admin.

    Tue 18th November, 2008

  3. Phill Sparks Phill Sparks wrote:

    Thanks to Ed for fixing this error in Phlickr. This blog post will remain for anyone else dabbling with cURL.

    Wed 10th December, 2008

  4. Jackson Jackson wrote:

    Hello!

    What if you need to send a post request and actually have values to send, but are still getting this error? If I change it to a get request (and don't pass any data), I don't get this error... but I need to pass data for the api I'm using. Any thoughts?

    Wed 24th December, 2008

  5. Phill Sparks Phill Sparks wrote:

    Hi Jackson, how is your post data formatted? CURL needs it like this: "Hello=World&Foo=Bar&Baz=Wombat"; perhaps you could paste some code for me to have a look at?

    Sun 28th December, 2008

  6. Euperia Euperia wrote:

    Thanks for explaining this. After a server upgrade a lot of our clients' sites stopped providing their content to Amazon and Play.com. Turns out that the previous versions worked with this bug but the upgraded versions wouldn't

    Wed 1st December, 2010

Post Comment

Comment

Only registered users can post comments, please or below: