Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MathPix-API: PHP sample is missing #3

Open
q2apro opened this issue Jul 24, 2018 · 3 comments
Open

MathPix-API: PHP sample is missing #3

q2apro opened this issue Jul 24, 2018 · 3 comments

Comments

@q2apro
Copy link

q2apro commented Jul 24, 2018

Could you add an example for PHP, I am stuck with this custom code:

$uploadedFile = file_get_contents('ztest.jpg');

$ch = curl_init();

curl_setopt_array($ch, array(
	CURLOPT_RETURNTRANSFER => true, // receive server response
	CURLOPT_URL => "https://api.mathpix.com/v3/latex",
	CURLOPT_POST => 1,
	CURLOPT_HTTPHEADER => array('Content-Type:application/json'),
	CURLOPT_POSTFIELDS => array(
		"app_id" => "trial",
		"app_key" => "34f1a4cea0eaca8540c95908b4dc84ab",
		"src" => "data:image/jpeg;base64,".base64_encode($uploadedFile), // data	
	)
));

$result = curl_exec($ch);

curl_close($ch);

// further processing
if($result == "OK") 
{
	// **** how to access the latex results?!
	var_dump('Okay');
} 
else 
{
	var_dump($result);
}

I guess the data should not be sent by "src" but differently using CURL.

@q2apro
Copy link
Author

q2apro commented Jul 24, 2018

I debugged by:

if(!curl_exec($ch)){
	die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}

which gave:

Error: "SSL certificate problem: unable to get local issuer certificate" - Code: 60

Then I uploaded the testing files to my server and got:

string(103) "{"error":"Invalid credentials","error_info":{"id":"http_unauthorized","message":"Invalid credentials"}}"

even though I have an active account and entered valid credentials. Later I saw the info in the dashboard, that we have to wait 2 min.

I waited 2 min, still not working.

@q2apro
Copy link
Author

q2apro commented Jul 24, 2018

I am not sure if I can send the image data by "src" => "data:image/jpeg;base64,".base64_encode($uploadedFile),.

The trial credentials do not work either!

Can you help me out?

@q2apro
Copy link
Author

q2apro commented Jul 25, 2018

Got it to run, just have seen now that I need to pass the credentials as HEADERS and not as post data.

This is the working code:

$uploadedFile = file_get_contents('test.jpg');

$data = array(
	"src" => "data:image/jpeg;base64,".base64_encode($uploadedFile), // data
);

$ch = curl_init();

curl_setopt_array($ch, array(
	CURLOPT_URL => "https://api.mathpix.com/v3/latex",
	CURLOPT_RETURNTRANSFER => true, // receive server response
	CURLOPT_POST => 1, // do post
	CURLOPT_HTTPHEADER => array('Content-Type: application/json', 'app_id: trial', 'app_key: 34f1a4cea0eaca8540c95908b4dc84ab'),
	CURLOPT_POSTFIELDS => json_encode($data),		
));

$result = curl_exec($ch);

if(!$result)
{
	die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}

curl_close($ch);

// further processing
var_dump($result);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant