繁体   English   中英

PHP Curl 发布并获取响应

[英]PHP Curl Post and Get Response

我有一个 shell 代码,如下所示。
"curl -X POST -F 'file=@textomate-api.pdf;type=text/plain' https://textomate.com/a/uploadMultiple "

此代码将文件发送到 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
  }
}

我想通过 PHP 发布文件并获得此响应或仅“charsNoSpacesNumber”的值。

你能帮我解决这个问题吗?

非常感谢

你可以这样做:

但请务必先检查他们的 T&C,因为我不确定他们是否免费提供此类服务。

还要确保包含一些错误/异常处理。

<?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']);

感谢您对 Bartosz 的支持。 这是我的代码,我还分享了代码和结果的图像。 我希望有足够的信息。

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

?>

在此处输入图片说明

更新后的代码如下,您可以在图像中看到结果。

我想在我的网站上使用这个 API 来计算文档的字符数。 我正在使用 Wordpress,API 提供程序为我们提供了 3 个选项,Java、PHP(Wordpress)和 Shell。 他们有一些适用于 Wordpress 的东西,但我不知道如何使用它。

您可以从这里访问所有文件。 https://github.com/zentaly/textomate-api如果你看看那里,你可以获得更多信息,也许你可以找到我更好的解决方案。

再次感谢您的支持,我很感激。

<?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.

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