Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my Twitter app returning a port error?

Tags:

php

oauth

twitter

I have made a Twitter app to simply post tweets from my web site, using the "twitteroauth" and "oauth" PHP scripts here.

Everything works, but my error logs are giving me this error:

Undefined index: port OAuth.php 383

Although this doesn't seem to be inhibiting my scripts from functioning, I'd like to keep my error logs free of noise. And avoid possible future issues.

Any idea why this is happening?

For reference the code in OAuth.php the error log is pointing to is this:

public function get_normalized_http_url() {
$parts = parse_url($this->http_url);
$port = @$parts['port'];       <-- Line 383
$scheme = $parts['scheme'];
$host = $parts['host'];
$path = @$parts['path'];
$port or $port = ($scheme == 'https') ? '443' : '80';
if (($scheme == 'https' && $port != '443')
|| ($scheme == 'http' && $port != '80')) {
$host = "$host:$port";
}
return "$scheme://$host$path";
}
like image 533
Questioner Avatar asked Sep 19 '11 10:09

Questioner


3 Answers

That is an error in the OAuth.php file, it accesses an index of an array w/o performing an index check.

Obviously the coder/ette who wrote this was so clever-clever to use the error suppression operator @ instead of doing a proper index check - being lazy (to assume the best).

Report this as a bug upstream, a fix is trivial:

$parts = parse_url($this->http_url) + array('port'=>NULL, 'path'=>NULL);

and removing the two @ operators.

like image 135
hakre Avatar answered Oct 15 '22 16:10

hakre


This happens because parse_url() is not guaranteed to return the port number (emphasis mine):

If the component parameter is omitted, an associative array is returned. At least one element will be present within the array.

Try using something like $port = (array_key_exists('port', $parts) ? $parts['port'] : 80); to hide the notice if you wish to not touch your error_reporting.

like image 5
Shadikka Avatar answered Oct 15 '22 16:10

Shadikka


This bug has been fixed. Here is the commit url:

https://github.com/christiandavid/twitteroauth/commit/dd944c8de3123ae5e0f380b4a907c92903059fae

like image 4
Rakesh Tembhurne Avatar answered Oct 15 '22 15:10

Rakesh Tembhurne