underscore encoding in urls

"Please leave a message at the beep, we will get back to you when your support contract expires."

Moderators: phlip, Moderators General, Prelates

User avatar
spacefem
Posts: 55
Joined: Sun Oct 26, 2008 12:09 pm UTC
Location: midwest
Contact:

underscore encoding in urls

Postby spacefem » Tue Feb 24, 2015 12:45 pm UTC

hey all! I'm trying to write a php script to fetch some XML files and having some inconsistencies. they're on livejournal... don't make fun of me but I still like livejournal and want to write some widgets for it.

I've tried two different ways in php to open these files...

first curl:
curl_init($url);

then xml parsing with DOM:
$xmlDoc = new DOMDocument();
$xmlDoc->load($url);

in both cases, some of the files I want give me a "failed to open" sort of error. The ones that do not work have one thing in common: underscores. But some of the files that work have underscores too. So I give up.

Examples of files that work, every time... I can open these just fine:
http://adefinitemaybe.livejournal.com/data/foaf.rdf
http://spacefem.livejournal.com/data/foaf.rdf
http://u_b_c.livejournal.com/data/foaf.rdf

Examples of files that totally do NOT work ever... I get a "not found" error in my script even though I can open them in a browser!
http://_frangible_.livejournal.com/data/foaf.rdf
http://i_love_freddie.livejournal.com/data/foaf.rdf
http://katie____.livejournal.com/data/foaf.rdf

there are no files that work sometimes. a given username will either always work or never work.

what's up with this... can you think of any way I should treat or encode underscores differently?

User avatar
Jplus
Posts: 1692
Joined: Wed Apr 21, 2010 12:29 pm UTC
Location: Netherlands

Re: underscore encoding in urls

Postby Jplus » Tue Feb 24, 2015 10:53 pm UTC

I have no idea what is causing your problem nor do I have any substantial experience with PHP, but you can always try replacing the underscores by the hex code %5f .
"There are only two hard problems in computer science: cache coherence, naming things, and off-by-one errors." (Phil Karlton and Leon Bambrick)

coding and xkcd combined

(Julian/Julian's)

User avatar
chridd
Has a vermicelli title
Posts: 779
Joined: Tue Aug 19, 2008 10:07 am UTC
Location: ...Earth, I guess?
Contact:

Re: underscore encoding in urls

Postby chridd » Wed Feb 25, 2015 6:58 am UTC

All of those URL's seem to work for me from PHP. Are you sure that $url contains what you think it does? (Perhaps it's encoded somehow and needs to be decoded.)

Also, it looks like LiveJournal treats underscores and hyphens interchangeably (at least, I get the same thing when I replace underscores with hyphens in my browser), so you could try using hyphens instead if you can't get it to work with underscores.

spacefem wrote:Examples of files that work, every time... I can open these just fine:
[...]
http://u_b_c.livejournal.com/data/foaf.rdf
...that has underscores, though.

Jplus wrote:I have no idea what is causing your problem nor do I have any substantial experience with PHP, but you can always try replacing the underscores by the hex code %5f .
If I try that in my browser, the page doesn't load (server not found). $ch = curl_init("http://%5ffrangible%5f.livejournal.com/data/foaf.rdf"); gives an "Unknown Journal" page for me.
~ chri d. d. /tʃɹɪ.di.di/ (Phonotactics, schmphonotactics) · they (for now, at least) · Forum game scores
mittfh wrote:I wish this post was very quotable...
flicky1991 wrote:In both cases the quote is "I'm being quoted too much!"


Return to “The Help Desk”

Who is online

Users browsing this forum: No registered users and 14 guests