Expect 100-continue Header Twitter API Error using PHP CURL

Twitter API

I was trying to develop a small application for Twitter API when I was faced with a very odd error by Twitter. Apparently, nowadays Twitter API does not like some of the cURL querying without the proper headers. Here's how you can fix that.

Just like ShoeMoney's app, my Twitter PHP Curl application was returning "HTTP/1.1 417 Expectation Failed".

The error looks like this:

<p>HTTP/1.1 417 Expectation Failed: The expectation given in the Expect request-header
field could not be met by this server.</p>
<p>The client sent<pre>
Expect: 100-continue
</pre>
but we only allow the 100-continue expectation.</p>

Shoemoney seems to have figured out the solution:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));

There are a lot of headers and curl_setopts you don't really need.

The real essentials for Twitter API in PHP is:

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_USERPWD, $login); // Authenticate
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

Although I use more options than that, this is all you really need.

Anonymous's picture

Ah, so that's why I couldn't

Ah, so that's why I couldn't make that twit app. I'll go test this out.

Dan Killam's picture

Nice, I'm sure this will help

Nice, I'm sure this will help a good amount of tweeps

Anonymous's picture

Hello thanks for this

Hello
thanks for this article, it will help me achieve my goals

Anonymous's picture

I call the following lines:

I call the following lines:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

curl_setopt($ch, CURLOPT_POST, 1);

$result = curl_exec($ch);

I get the status code 417. If I replace the above lines with the given above by you I get the error code 401. Please help. Getting crazy.

Anonymous's picture

Thanks for solution.

Thanks for solution.

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account, used to display your avatar.