PHP Curl 发布并获得响应

我有一个如下的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”的值。


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


喵喵时光机
浏览 139回答 3
3回答

慕桂英546537

更新的代码如下,您可以在图像中看到结果。我想在我的网站上使用这个 API来计算文档的字符数。我正在使用 Wordpress,API 提供者为我们提供了 3 个选项,Java、PHP (Wordpress) 和 Shell。他们有一些适用于 Wordpress 的东西,但我不知道如何使用它。您可以从这里访问所有文件。&nbsp;https://github.com/zentaly/textomate-api&nbsp;如果你看看那里,你可以获得更多信息,也许你可以找到我更好的解决方案。再次感谢您的支持,我很感激。<?phpif(is_callable('curl_init')){&nbsp;&nbsp; &nbsp; echo "curl is active";&nbsp;} else {&nbsp;&nbsp; &nbsp; echo "curl is passive";&nbsp;}//Initialise the cURL var$ch = curl_init();//Get the response from cURLcurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//Set the Urlcurl_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));&nbsp;$path = 'textomate-api.pdf';$file = curl_file_create($path, 'application/pdf', 'file');//Create a POST array with the file in it$postData = array(&nbsp; &nbsp; '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);?>

www说

这是我的代码,我还分享了代码和结果的图像。我希望有足够的信息。<?phpif(is_callable('curl_init')){&nbsp;&nbsp; &nbsp; echo "curl is active";&nbsp;} else {&nbsp;&nbsp; &nbsp; echo "curl is passive";&nbsp;}//Initialise the cURL var$ch = curl_init();//Get the response from cURLcurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//Set the Urlcurl_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(&nbsp; &nbsp; '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));?>

有只小跳蛙

你可以这样做:但请务必先检查他们的 T&C,因为我不确定他们是否免费提供此类服务。还要确保包括一些错误/异常处理。<?php//Initialise the cURL var$ch = curl_init();//Get the response from cURLcurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//Set the Urlcurl_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(&nbsp; &nbsp; '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']);
打开App,查看更多内容
随时随地看视频慕课网APP