Posts tagged: curl

PHP, curl and SSL23_GET_SERVER_HELLO

Using the following curl block I was getting an unexpected response from the server.

1
2
3
4
5
6
7
$ch = curl_init($post_url);
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, "orderXML=".$xmlRequest );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_TIMEOUT, 3000 );
curl_setopt( $ch, CURLOPT_VERBOSE, 1 );
$xmlResponse = curl_exec( $ch );

Pretty simple call. It’s attempting to communicate with an url over https which is where the problem lies. Here is the response I was receiving.

1
2
Curl error number: 35
Curl error: error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure

Simple call. Confusing problem. Simple solution. Add the following line before the curl_exec.

1
curl_setopt($ch, CURLOPT_SSLVERSION, 1);

Checking the documentation, it says this: CURLOPT_SSLVERSION – The SSL version (2 or 3) to use. By default PHP will try to determine this itself, although in some cases this must be set manually.

I tried setting the value to 2 but it didn’t work.

1
2
Curl error number: 4
Curl error: OpenSSL was built without SSLv2 support

And setting it to 3 didn’t either.

1
2
Curl error number: 35
Curl error: error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number

I wish I could tell you why setting it to 1 worked for me, but I don’t understand it. It just works for me where the documented values do not.

Yet Another Command-Line Twitter

Recently this article made it to the front page of digg. It’s yet another article on how to update your Twitter status from the command line. It covers the bases of how to do it, but I still prefer Wayne’s way over at Fsckin.com

First, the script requires cURL to be installed. So install it if it’s not already on your system.

sudo apt-get update
sudo apt-get install curl

I made some minor revisions to the Fsckin way to do it after reading the cURL man page and finding that the –basic flag wasn’t required etc.

It boils down to this

curl --user "username:password" \
--data-ascii "status=`echo $@|tr ' ' '+'`" \
"http://twitter.com/statuses/update.xml"

Just copy the text into a file, I’ve called mine “tweet”, and change the username and password to your own information. Note, the text is there for anyone to see who has access to your computer. This is significant as your Twitter password is saved inside that file.

Change the file permission to make it executable:

chmod 755 tweet

And make a tweet on your Twitter profile

./tweet This message is what everyone will see.

which would post the message for everyone to see. You’ll end up getting some output from cURL. So long as it doesn’t come back with a short message which includes

<error>Could not authenticate you.</error>

then we can assume all went well.

Advanced Stuff

You might find that you want to be able to call the “tweet” utility from outside the directory it lives in. You can include the directory in your system path by adding the path to the bottom of your .bashrc

PATH=$PATH:$HOME/path/to/the/script
export PATH

So the next time you log in you can just type, from any directory,

tweet whatever the message is

and your twitter profile will be updated.

Personally I like to keep my scripts in a hidden directory in my /home. Something like /home/myusername/.my_scripts (or shorthand: ~/.my_scripts)

Staypressed theme by Themocracy