繁体   English   中英

如何完成此PHP curl请求以使用Python从API获得响应?

[英]How can I accomplish this PHP curl request to get response from an API with Python?

考虑以下PHP代码,该代码汇总了URL中的文本:

$url = 'http://www.my-website.com/article-123.php';
$webService = 'https://resoomer.pro/websummarizer/';
$datasPost = 'API_KEY=MY_API_KEY&url='.$url;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $webService);
curl_setopt($ch,CURLOPT_POST, 2);
curl_setopt($ch,CURLOPT_POSTFIELDS, $datasPost);
$result = curl_exec($ch);
curl_close($ch);
echo $result;

它返回如下的JSON结果:

{
"ok":1,
"message":"Resoomer ok",
"longText":{
"size":45,
"content":"Votre texte résumé à 45%..."
},
"mediumText":{
"size":25,
"content":"Votre texte résumé à 25%..."
},
"smallText":{
"size":15,
"content":"Votre texte résumé à 15%..."
},
"codeResponse":200
}

我试过了:

response=requests.get("https://resoomer.pro/websummarizer/?API_KEY=MY_API_KEY&url=https://en.wikipedia.org/wiki/Statistical_hypothesis_testing")

这工作得很好。

因此,我尝试了Resoomer API文本摘要程序:

$MyText = 'My text plain...';
$webService = 'https://resoomer.pro/summarizer/';
$datasPost = 'API_KEY=MY_API_KEY&text='.$MyText;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $webService);
curl_setopt($ch,CURLOPT_POST, 2);
curl_setopt($ch,CURLOPT_POSTFIELDS, $datasPost);
$result = curl_exec($ch);
curl_close($ch);
echo $result;

这将给出JSON响应,例如:

{
"ok":1,
"message":"ok",
"text":{
"size":45,
"total_words":219,
"content":"Your text summarize to 45%..."
},
"codeResponse":200
}

我想在Python 3.5或Python 3.6中完成此操作。

我试过了:

url = 'https://resoomer.pro/summarizer/'
Text="'A statistical hypothesis, sometimes called confirmatory data analysis, is a hypothesis that is testable on the basis of observing a process that is modeled via a set of random variables.[1] A statistical hypothesis test is a method of statistical inference. Commonly, two statistical data sets are compared, or a data set obtained by sampling is compared against a synthetic data set from an idealized model. A hypothesis is proposed for the statistical relationship between the two data sets, and this is compared as an alternative to an idealized null hypothesis that proposes no relationship between two data sets. The comparison is deemed statistically significant if the relationship between the data sets would be an unlikely realization of the null hypothesis according to a threshold probability—the significance level. Hypothesis tests are used in determining what outcomes of a study would lead to a rejection of the null hypothesis for a pre-specified level of significance. The process of distinguishing between the null hypothesis and the alternative hypothesis is aided by identifying two conceptual types of errors, type 1 and type 2, and by specifying parametric limits on e.g. how much type 1 error will be permitted.An alternative framework for statistical hypothesis testing is to specify a set of statistical models, one for each candidate hypothesis, and then use model selection techniques to choose the most appropriate model.[2] The most common selection techniques are based on either Akaike information criterion or Bayes factor.Confirmatory data analysis can be contrasted with exploratory data analysis, which may not have pre-specified hypotheses.'"
datasPost = r'API_KEY=MY_API_KEY&text='+Text
response = requests.get(url+r"?"+datasPost)

没用

有谁知道如何解决这个问题?

我检查了文本中是否有任何转义,但没有发现任何内容。

有关API的更多参考,请访问resoomer API

您对请求的处理方式有所不同:

  1. 这应该是一个POST,而不是GET - requests.post

  2. 您想将api键和文本作为POST数据而不是作为url参数发送。

它应该改为工作:(或至少与您使用PHP / curl所做的匹配)

requests.post(url, data=datasPost)

如果仍然收到无效的令牌响应,则必须仔细检查您实际发送的是预期的内容。

我设法使用pycurl解决了这个问题

这是jupyter笔记本的代码-python 3.x

安装pycurl

!apt-get install libcurl4-openssl-dev
!apt-get install libssl-dev
!pip install pycurl

检索响应的代码

import pycurl
from io import BytesIO
datasPost='API_KEY='+API+'&text='+str(article)
response= BytesIO()
c = pycurl.Curl()
c.setopt(pycurl.URL, 'https://resoomer.pro/summarizer/')
c.setopt(pycurl.POST, 2)
c.setopt(pycurl.POSTFIELDS, datasPost)
c.setopt(c.WRITEFUNCTION, response.write)
c.perform()
content = response.getvalue().decode('UTF-8')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM