How can I query Google Spreadsheets API with a string value?
I am using Zend_Gdata_SpreadsheetsListQuery. In PHP my query is:
"confirmation=$confirmation"
The problem is that $confirmation = 'AB-CD-EFG-012345';
Apparently the hyphens are causing problems with the query and the exception thrown is:
Uncaught exception 'Zend_Gdata_App_HttpException' with message 'Expected response code 200, got 400 Parse error: Invalid token enc开发者_StackOverflow社区ountered'
How can I quote or escape the value to not cause parse errors? Single quotes cause the same error.
Edit: When I was testing with double quotes there was user error. Double quotes work.
As it turns out I had two spreadsheets, one for dev and one for production and I was not querying the one I thought I was.. This solution works:
"confirmation=\"$confirmation\""
It's worth noting, that you have to be careful with escaping. I tried this and didn't get any joy. I tried many combinations along these lines:
$query->setSpreadsheetQuery("name=\"Andrew John\"");
but to no avail. Running the traffic through an HTTP Inspector (and setting up your Zend HTTP Client to run through a proxy is another thing worth looking at!) I could see that the generated URLs from this work looking like this:
http://spreadsheets.google.com:80/feeds/list/spreadsheetkey/od6/private/full?sq=name%3D%5C%22Andrew+John%5C%22
This was resulting in Invalid Token (Status 400) errors. It didn't like the %5C%22 (\") escape sequence, but where was it getting it from?!
It turns out that there is a "parse_str" embedded deep in Zends code, which was adding slashes to the query string because for some reason (Ubuntu default php config?) magic_quotes_gpc was set to On in the php.ini file.
Turning this off allowed the generated URLs to revert to:
http://spreadsheets.google.com:80/feeds/list/spreadsheetkey/od6/private/full?sq=name%3D%22Andrew+John%22
and everything started working.
Incidentally, I had to resort to running it through an HTTP Inspector, because doing:
echo $query->getQueryUrl();
incorrectly reported that it was sending the string above without the %5C before each quote! Just goes to show, it never pays to trust what your code tells you...
So, if you have issues with structured queries in Zend framework's Gdata classes and just adding quotes doesn't solve it for you, have a quick look to see if you've got magic_quotes_gpc set to On!
I have tried to research the Zend library for this, but am having no joy.
Could it be something as simple as wrapping the value in apostrophes?
Your code:
"confirmation=$confirmation"
Possible code for testing:
"confirmation='$confirmation'"
Just a random suggestion...
You can use urlencode
. However, I would expect the Zend API to handle that for you, so check that you're using it correctly.
my solution:
get rows which does not match the query exactly - by the column which doesnt seem to have forbidden letters/signs. and in php script do filtering of the rows.
精彩评论