我在PHP中通过file_get_contents使用YQL来发送查询。我使用YQL进行术语提取,所以我的查询包含大量文本。不幸的是,这会使URL过长并返回一个错误。如果我使用的文本数量要少得多,它工作得很好。
是我在YQL上使用SELECT语句和GET的唯一方式吗?除了使用更少的文本之外,我还有什么其他选择?
发布于 2011-01-26 05:00:32
是我在带有GET的YQL上使用SELECT语句的唯一方法,除了使用更少的文本之外,我还有什么其他选择?
正如其他人所说,您可以使用POST请求而不是GET请求。下面是一个在流上下文中使用file_get_contents()的示例。可以发出POST请求的cURL或任何其他远程内容获取代码也可以很好地工作。
$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);发布于 2011-01-24 22:36:32
为什么不使用CURL而使用get变量进行查询呢?
$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);https://stackoverflow.com/questions/4644553
复制相似问题