API & Curl: Status update result: http_code =>0!

978 views
Skip to first unread message

nordmograph

unread,
Jul 12, 2009, 1:15:09 AM7/12/09
to Twitter Development Talk
Hi there , I'm new to this group, so hello everyone,
I'm tryng to set my first (php) use of the twitter API using CUrl and
I'm experiencing a strange behaviour:

I get this http_code zero when my post has been added and also when
I can't authentificate. I read a previous post on this group saying it
was caused by:

curl_setopt($ch, CURLOPT_POST, 1);

That post said that commenting out this line (or setting to false)
would fix it.
But if I do so I get a good 401 for password error which makes me
happy
but still have a 400: <error>This method requires a POST.</error>
when it should post fine...

I can't get my 200 even if the update is posted to my twitter
succesfully using that curlopt_post.

Any help greatly appreciated!
thks

Arnaud Meunier

unread,
Jul 12, 2009, 6:43:43 AM7/12/09
to twitter-deve...@googlegroups.com
Set CURLOPT_VERBOSE option to 1, and copy / paste the output here
(think to hide the auth part).

2009/7/12, nordmograph <adro...@gmail.com>:


--
Arnaud Meunier
Twitoaster | http://twitoaster.com

Dewald Pretorius

unread,
Jul 12, 2009, 9:03:08 AM7/12/09
to Twitter Development Talk
An HTTP code in cURL of 0 usually means your request is being denied
by Twitter at the network equipment level. In other words, your
connection is refused. This sometimes happens when the Twitter network
is overloaded.

nordmograph

unread,
Jul 13, 2009, 12:57:52 AM7/13/09
to Twitter Development Talk
Hi thank you both of you for your quick replies!
Yes Zero means denied but the tweet is posted and listed fine
It is not sometimes, it is always... unfortunately.
CURLOPT_VERBOSE option is 1 and here is what my postToTwitter function
looks like:

function postToTwitter($username,$password,$message){
$host = "http://twitter.com/statuses/update.xml?
status=".urlencode(stripslashes(urldecode($message)));
$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);
// Look at the returned header
$resultArray = curl_getinfo($ch);
curl_close($ch);
if($resultArray['http_code'] == "200")
$twitter_status=_CBTW_TWEETSENT;
else
$twitter_status=_CBTW_ERRORRETRY;
}

and then

if(isset($_POST['twitter_msg']) && !isset($error)){
$return .='<div class="msg">'. $twitter_status .'</div>';
$twitter_message=$_POST['twitter_msg'];
if(strlen($twitter_message)<1)
$error=1;
else
$twitter_status=postToTwitter($twittername, $twitterpass,
$twitter_message);
}

the tweet is posted fine to my twitter page
and here is the result:

Array
(
[url] => http://twitter.com/statuses/update.xml?status=so+hot+in+here
[http_code] => 0
[header_size] => 0
[request_size] => 211
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.254544
[namelookup_time] => 0.081081
[connect_time] => 0.254497
[pretransfer_time] => 0.254505
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => 0
[upload_content_length] => -1
[starttransfer_time] => 0.254539
[redirect_time] => 0
)

Thanks a lot for your help!

Arnaud Meunier

unread,
Jul 13, 2009, 7:24:40 AM7/13/09
to twitter-deve...@googlegroups.com
With CURLOPT_VERBOSE option set to 1, you should see the whole http communication. It would help to understand what's going on.
You're probably not outputing errors (don't see any http trace) so try to set the CURLOPT_STDERR option to some file PHP can write to, and send us its content (remind to hide the auth part).

-- 
Arnaud Meunier
Twitoaster | http://twitoaster.com


2009/7/13 nordmograph <adro...@gmail.com>

nordmograph

unread,
Jul 14, 2009, 12:56:56 AM7/14/09
to Twitter Development Talk
OK , I was testing on a shared host
I finally tested locally on a win32 with Curl and it worked fine
Twitter sending back a http_code 200 on successfull tweet.

Any Idea why the same script used on my host receives a http_code 0
whatever happens?
Thanks a lot!

nordmograph

unread,
Jul 14, 2009, 8:12:57 AM7/14/09
to Twitter Development Talk
So as I said when I test my script localy on my win32

it works fine I get this:

Array
(
[url] => http://twitter.com/statuses/update.xml?status=It%27s+made+of+a+module+and+a+profile+application
[content_type] => application/xml; charset=utf-8
[http_code] => 200
[header_size] => 890
[request_size] => 246
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.885
[namelookup_time] => 0.076
[connect_time] => 0.278
[pretransfer_time] => 0.278
[size_upload] => 0
[size_download] => 1805
[speed_download] => 2039
[speed_upload] => 0
[download_content_length] => 1805
[upload_content_length] => -1
[starttransfer_time] => 0.884
[redirect_time] => 0
)

<?xml version="1.0" encoding="UTF-8"?>
<status>
<created_at>Tue Jul 14 12:06:20 +0000 2009</created_at>
<id>2631102072</id>
<text>It's made of a module and a profile application</text>
<source>&lt;a href="http://apiwiki.twitter.com/"&gt;API&lt;/a&gt;</
source>
<truncated>false</truncated>
<in_reply_to_status_id>
</in_reply_to_status_id>
<in_reply_to_user_id>
</in_reply_to_user_id>
<favorited>false</favorited>
<in_reply_to_screen_name>
</in_reply_to_screen_name>
<user>
<id>35691583</id>
<name>nordmograph</name>
<screen_name>nordmograph</screen_name>
<location>Tokyo</location>
<description>
</description>
<profile_image_url>http://s3.amazonaws.com/twitter_production/
profile_images/303127750/avatar_normal.png</profile_image_url>
<url>http://nordmograph.com</url>
<protected>false</protected>
<followers_count>47</followers_count>
<profile_background_color>FFFFFF</profile_background_color>
<profile_text_color>333333</profile_text_color>
<profile_link_color>0ba6de</profile_link_color>
<profile_sidebar_fill_color>cccccc</profile_sidebar_fill_color>
<profile_sidebar_border_color>cccccc</
profile_sidebar_border_color>
<friends_count>35</friends_count>
<created_at>Mon Apr 27 06:56:40 +0000 2009</created_at>
<favourites_count>0</favourites_count>
<utc_offset>-10800</utc_offset>
<time_zone>Greenland</time_zone>
<profile_background_image_url>http://s3.amazonaws.com/
twitter_production/profile_background_images/22071817/
twitterbg_nordmograph.jpg</profile_background_image_url>
<profile_background_tile>false</profile_background_tile>
<statuses_count>34</statuses_count>
<notifications>false</notifications>
<verified>false</verified>
<following>false</following>
</user>
</status>

but no password to hide, I'm not sure I get what you mean, how can I
give you mor infos?

lda

unread,
Jul 16, 2009, 6:23:40 PM7/16/09
to Twitter Development Talk
I have same issue...

with curl_setopt($ch, CURLOPT_POST, 1);

i get this

* About to connect() to twitter.com port 80 (#0)
* Trying 168.143.162.100... * connected
* Connected to twitter.com (168.143.162.100) port 80 (#0)
* Server auth using Basic with user 'xxxxx'
> POST /statuses/update.xml?status=http%3A%2F%2Flocalhost%2Fent_umgebung%2Findex.php%3Foption%3Dcom_content%26view%3Darticle%26id%3D99+adasd HTTP/1.1

Authorization: Basic xxxxx
Host: twitter.com

Accept: */*

Content-Length: 0

Content-Type: application/x-www-form-urlencoded



* Closing connection #0
* Failed to open/read local data from file/application

but the tweet shows up at twitter.com

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
with curl_setopt($ch, CURLOPT_POST, 0);

i get this

* About to connect() to twitter.com port 80 (#0)
* Trying 168.143.162.116... * connected
* Connected to twitter.com (168.143.162.116) port 80 (#0)
* Server auth using Basic with user 'xxx'
> GET /statuses/update.xml?status=http%3A%2F%2Flocalhost%2Fent_umgebung%2Findex.php%3Foption%3Dcom_content%26view%3Darticle%26id%3D100+asdsd HTTP/1.1

Authorization: Basic xxxx
Host: twitter.com

Accept: */*



< HTTP/1.1 400 Bad Request

< Date: Thu, 16 Jul 2009 22:19:04 GMT

< Server: hi

< Last-Modified: Thu, 16 Jul 2009 22:19:04 GMT

< Status: 400 Bad Request

< X-RateLimit-Limit: 150

< X-RateLimit-Remaining: 148

< Pragma: no-cache

< Cache-Control: no-cache, no-store, must-revalidate, pre-check=0,
post-check=0

< Content-Type: application/xml; charset=utf-8

< X-RateLimit-Reset: 1247783018

< Content-Length: 256

< Expires: Tue, 31 Mar 1981 05:00:00 GMT

< X-Revision: f4f87122b27713088b558c6b57aad4f73e83a56d

< X-Transaction: 1247782744-54919-16662

< Set-Cookie: lang=en; path=/

< Set-Cookie:
_twitter_sess=BAh7CToTcGFzc3dvcmRfdG9rZW4iLWI3YmU3OTRiMDVjNzRkNmYwMWU5NmVk
%250AOTc0ZWFjY2M4Y2MzNjliOWY6CXVzZXJpBCwYXwM6B2lkIiViZWQxNTkzMWZm
%250AMjFiYWZlZWIwYjI3NmZmM2VlOGNmOCIKZmxhc2hJQzonQWN0aW9uQ29udHJv
%250AbGxlcjo6Rmxhc2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA%253D
%253D--833c18afbc67a08e2c62ee0e278ac6eafb304123; domain=.twitter.com;
path=/

< Vary: Accept-Encoding

< Connection: close

<

* Closing connection #0

and the twitter dont show up at twitter.com

lda

unread,
Jul 20, 2009, 11:23:26 AM7/20/09
to Twitter Development Talk
nobody an idea?

Chad Etzel

unread,
Jul 20, 2009, 2:27:57 PM7/20/09
to twitter-deve...@googlegroups.com
Ah, I think I may have an idea...

You both are setting the status in the query string (more like a GET
request) and setting curl_setopt($ch, CURLOPT_POST, 1).

But, It doesn't look like either of you are setting a POSTFIELDS
option. I got http_code=>0 sometime ago when I realized I was not
sending a POST body along with a POST request.

So, try setting curl_setopt(CURLOPT_POSTFIELDS, ""); (yes, it's just
an empty string, but it creates the content-length header in the
request) and then see what happens.

-Chad

On Mon, Jul 20, 2009 at 11:23 AM, lda<sv...@schultschik.de> wrote:
>
> nobody an idea?

lda

unread,
Jul 24, 2009, 5:57:58 AM7/24/09
to Twitter Development Talk
Thx worked

On 20 Jul., 20:27, Chad Etzel <jazzyc...@gmail.com> wrote:
> Ah, I think I may have an idea...
>
> You both are setting the status in the query string (more like a GET
> request) and setting curl_setopt($ch, CURLOPT_POST, 1).
>
> But, It doesn't look like either of you are setting a POSTFIELDS
> option.  I gothttp_code=>0sometime ago when I realized I was not
> sending a POST body along with a POST request.
>
> So, try setting curl_setopt(CURLOPT_POSTFIELDS, "");  (yes, it's just
> an empty string, but it creates the content-length header in the
> request) and then see what happens.
>
> -Chad
>
> On Mon, Jul 20, 2009 at 11:23 AM, lda<s...@schultschik.de> wrote:
>
> > nobody an idea?
Reply all
Reply to author
Forward
0 new messages