开发者

PHP/YQL/GET: URL too long

开发者 https://www.devze.com 2023-02-04 09:55 出处:网络
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

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号