1

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

3 Answers 3

1

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']);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your support. But it didn't work for me. Sample file and index.php page is in the same directory. I try to indicate the path both '/test.pdf' and 'test.pdf' but it returned null value. They provide this service for free. When I try to run the code I shared first from shell it worked. I just wanted to run the same code via PHP but I still don't know how to do it. I hope you have another solution for me :( Thanks a lot
Hmm sorry for such a trivial idea, but have you tried checking if php can see the file? Try var_dumping file_exists($path) and see the result. Also what response you get from their service (try var_dumping) the response?
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

5 Comments

Let us try two more things - add: curl_setopt($ch, CURLOPT_VERBOSE, 2); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); and after curl_exec: var_dump(curl_errno($ch)); var_dump(curl_error($ch)); Change also: $response = json_decode(curl_exec($ch), true); to: $response = curl_exec($ch); var_dump($response); $response = json_decode($ch);
Also var_dump the file: var_dump($file);
I shared the last situation. I hope you can check the results
There was a typo in my last comment, it should be $response = json_decode($response);. Sorry for that.
It seems that we also misunderstood each other in the part about curl_exec... Let me give a sample code - one sec.
0

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

1 Comment

Please check this: pastebin.com/VJqaj1Vi - it covers parts where we have misunderstood each other, also has my contact details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.