[英]PHP Curl Post and Get Response
I have a shell code as below.我有一个 shell 代码,如下所示。
"curl -X POST -F 'file=@textomate-api.pdf;type=text/plain' https://textomate.com/a/uploadMultiple " "curl -X POST -F 'file=@textomate-api.pdf;type=text/plain' https://textomate.com/a/uploadMultiple "
This code send a file to the URL and get the response below.此代码将文件发送到 URL 并获得以下响应。
{
"countedFiles": [
{
"wordsNumber": 340,
"charsNumber": 2908,
"charsNoSpacesNumber": 2506,
"wordsNumberNN": 312,
"charsNumberNN": 2755,
"charsNoSpacesNumberNN": 2353,
"md5": null,
"fileName": "textomate-api.pdf",
"error": null
}
],
"total": {
"wordsNumber": 340,
"charsNumber": 2908,
"charsNoSpacesNumber": 2506,
"wordsNumberNN": 312,
"charsNumberNN": 2755,
"charsNoSpacesNumberNN": 2353,
"md5": null
}
}
And I want to post a file via PHP and get this response or only value of "charsNoSpacesNumber".我想通过 PHP 发布文件并获得此响应或仅“charsNoSpacesNumber”的值。
Could you please help me with this?你能帮我解决这个问题吗?
Thanks a lot非常感谢
You can do it as follows:你可以这样做:
YET be sure to first check their T&C as I am not sure if they provide such a service for free.但请务必先检查他们的 T&C,因为我不确定他们是否免费提供此类服务。
Also be sure to include some error / exceptions handling.还要确保包含一些错误/异常处理。
<?php
//Initialise the cURL var
$ch = curl_init();
//Get the response from cURL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//Set the Url
curl_setopt($ch, CURLOPT_URL, 'https://textomate.com/a/uploadMultiple');
$path = '/path/to/your/file.pdf';
$file = curl_file_create($path, 'application/pdf', 'file');
//Create a POST array with the file in it
$postData = array(
'file' => $file,
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
// Execute the request, decode the json into array
$response = json_decode(curl_exec($ch), true);
var_dump($response['total']['charsNoSpacesNumber']);
Thanks for your support Bartosz.感谢您对 Bartosz 的支持。 This is my code and I also shared an image of the code and results.
这是我的代码,我还分享了代码和结果的图像。 I hope there are enough information.
我希望有足够的信息。
<?php
if(is_callable('curl_init')){
echo "curl is active";
} else {
echo "curl is passive";
}
//Initialise the cURL var
$ch = curl_init();
//Get the response from cURL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//Set the Url
curl_setopt($ch, CURLOPT_URL, 'https://textomate.com/a/uploadMultiple');
$path = 'textomate-api.pdf';
$file = curl_file_create($path, 'application/pdf', 'file');
//Create a POST array with the file in it
$postData = array(
'file' => $file,
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
// Execute the request, decode the json into array
$response = json_decode(curl_exec($ch), true);
var_dump($response['total']['charsNoSpacesNumber']);
var_dump($response);
var_dump(file_exists($path));
?>
Updated code is below and you can see the results in the image.更新后的代码如下,您可以在图像中看到结果。
I want to use this API on my website to be able to calculate character count of documents.我想在我的网站上使用这个 API 来计算文档的字符数。 I am using Wordpress and API provider gives us 3 options, Java, PHP (Wordpress) and Shell.
我正在使用 Wordpress,API 提供程序为我们提供了 3 个选项,Java、PHP(Wordpress)和 Shell。 They have something for Wordpress but I don't know how to use it.
他们有一些适用于 Wordpress 的东西,但我不知道如何使用它。
You can reach all the files from here.您可以从这里访问所有文件。 https://github.com/zentaly/textomate-api If you take a look there, you can get more information and maybe you can find me a better solution.
https://github.com/zentaly/textomate-api如果你看看那里,你可以获得更多信息,也许你可以找到我更好的解决方案。
And again thank you so much for your support, I appreciate it.再次感谢您的支持,我很感激。
<?php
if(is_callable('curl_init')){
echo "curl is active";
} else {
echo "curl is passive";
}
//Initialise the cURL var
$ch = curl_init();
//Get the response from cURL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//Set the Url
curl_setopt($ch, CURLOPT_URL, 'https://textomate.com/a/uploadMultiple');
curl_setopt($ch, CURLOPT_VERBOSE, 2);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec: var_dump(curl_errno($ch));
var_dump(curl_error($ch));
$path = 'textomate-api.pdf';
$file = curl_file_create($path, 'application/pdf', 'file');
//Create a POST array with the file in it
$postData = array(
'file' => $file,
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
// Execute the request, decode the json into array
$response = curl_exec($ch);
var_dump($response);
$response = json_decode($ch);
var_dump($response['total']['charsNoSpacesNumber']);
var_dump($response);
var_dump(file_exists($path));
var_dump($file);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.