簡體   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