PHP Curl Post and Get Response

I have a shell code as below.
"curl -X POST -F '[email protected];type=text/plain' https://textomate.com/a/uploadMultiple"

This code send a file to the URL and get the response below.

{
  "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".

Could you please help me with this?

Thanks a lot

Upvotes: 1

Views: 3116

Answers (3)

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. I am using Wordpress and API provider gives us 3 options, Java, PHP (Wordpress) and Shell. They have something for Wordpress but I don't know how to use it.

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.

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

?>

codesandresults

Upvotes: 0

Thanks for your support 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));

?>

enter image description here

Upvotes: 0

Bartosz Pachołek
Bartosz Pachołek

Reputation: 1308

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.

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

Upvotes: 1

Related Questions