Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python完成这个PHP curl请求以从API获取响应?_Php_Python 3.x_Api_Curl_Python Requests - Fatal编程技术网

如何使用Python完成这个PHP curl请求以从API获取响应?

如何使用Python完成这个PHP curl请求以从API获取响应?,php,python-3.x,api,curl,python-requests,Php,Python 3.x,Api,Curl,Python Requests,考虑以下总结URL文本的PHP代码: $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

考虑以下总结URL文本的PHP代码:

$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")
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)
这个很好用

所以我尝试了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
}
我想在Python3.5或Python3.6中实现这一点

我试过:

response=requests.get("https://resoomer.pro/websummarizer/?API_KEY=MY_API_KEY&url=https://en.wikipedia.org/wiki/Statistical_hypothesis_testing")
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的更多参考信息,请访问。

我使用pycurl解决了这个问题

以下是jupyter笔记本的代码-Python3.x

安装pycurl 检索响应的代码
您在请求方面做了一些不同的事情:

这应该是一篇文章,而不是GET-requests.POST

您希望将api键和文本作为POST数据发送,而不是作为url参数发送

这应该是可行的:或者至少与您使用PHP/curl所做的工作相匹配

requests.post(url, data=datasPost)

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

它不起作用。-你这是什么意思。服务是否返回了一些错误?您是否从代码中获取本地异常?您需要向我们提供有关尝试时具体发生的情况的更多详细信息。返回了一个状态无效令牌。它看起来像是来自您正在调用的服务的答案,您是否尝试添加自己的API密钥?“API_密钥”之前的r是故意的?