I've found answers that are similar to the one I'm looking for, but not one that completely addresses my problem.
A user clicks a link on my page which takes them to Google Books and does a search. However, if the first word in the title of the book contains a contraction, everything gets thrown off and none of my corrections seem to work.
Example: The link on my page looks like this:
mysite.com/book-search?bookauthor=Lance Armstrong&booktitle=It's Not About the Bike
However, when I run this:
$bookTitle = $_GET['booktitle']
I get the same title with the apostrophe in "It's" removed. And that's enough to completely throw off the Google Books Search and return no results.
The endpoint in the cURL session that does the search looks like this:
https://www.googleapis.com/books/v1/volumes?q=intitle:' . $bookTitle . '+inauthor:' . $bookAuthor;
Try it yourself -
https://www.googleapis.com/books/v1/volumes?q=intitle:It%27s+Not_About+The+Bike+inauthor:Lance+Armstrong
Returns a completely different result than
https://www.googleapis.com/books/v1/volumes?q=intitle:Its+Not_About+The+Bike+inauthor:Lance+Armstrong
You'd think that if I urlencoded the strings, that would solve the problem. But it doesn't. Still, for a reason that I can't fathom, when I do this:
$google_endpoint_a = 'https://www.googleapis.com/books/v1/volumes?q=intitle:' . urlencode($bookTitle) . '+inauthor:' . urlencode($bookAuthor);
...the urlencoding gets stripped out when it's passed to the GoogleAPI and I get a zero result search again.
Can somebody help me wade through this? I've tried everything I can think of.
OH - and here's the bonus - this ONLY happens if the first word in the string contains the apostrophe. So it happens with book titles like "It's Not About The Bike" and "Don't Blink". Apostrophes in other words don't seem to matter. (Like author Bill O'Reilly, for example.)
Is there an easy way to remove the first word from the string IF it contains an apostrophe? Or is there a better way?
The Really Ugly Solution:
So PHP passes $_GET variables through urldecode(). So even though it's ugly, here's what I ended up doing that solved the problem, even though I hope someone will come up with a better way.
Before I passed the variable through the URL, I replaced the apostrophe with a string of gibberish, like this:
$booktitle = str_replace("'", "W5x32RS97", $booktitle);
Then ran this:
$bookTitleTemp = $_GET('booktitle');
$bookTitle = str_replace("W5x32RS97", "'", $bookTitleTemp);
And suddenly, everything works. But there's GOT to be a better way.
Hadn't you try to subtitute the ' by \' in your title string? I mean, instead of: It's Not About the Bike, using this: It\'s Not About the Bike
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With