PHP/YQL/GET: URL too long
I'm using YQL in PHP with file_get_contents to send a query. I'm using YQL for term extraction so my query contains a large amount of text. Unfortunately this renders the URL too long and returns an error. It works fine if I use a much smaller amoun开发者_StackOverflow中文版t of text.
Is the only way I can use a SELECT statement on YQL with GET, and what other options do I have besides using a smaller amount of text?
Is the only way I can use a SELECT statement on YQL with GET, and what other options do I have besides using a smaller amount of text?
As others have said, you can use a POST request instead of GET. Below is an example using file_get_contents()
with a stream context. cURL or any other remote-content-fetching code which can issue POST requests would also work fine.
$ctx = stream_context_create(array('http' => array(
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => http_build_query(array(
'context' => $my_really_really_huge_context,
'query' => $query,
'format' => 'json',
'q' => 'SELECT * FROM search.termextract WHERE context=@context and query=@query'
))
)));
$json = file_get_contents('http://query.yahooapis.com/v1/public/yql', false, $ctx);
Why don't you use CURL instead of querying with the get variable?
$c = curl_init("http:/query.yahooapis.com/v1/public/yql?q=myverylongquery&format=json");
curl_setopt($c, CURLOPT_RETURNTRANSFERT, 1); // returns the data into the variable
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 20); // query times out after 20 seconds
$data = json_decode(curl_exec($c)); // I asked for data format to be in json in the query
curl_close($c);
精彩评论