将上传的文档转换为二进制/ascii 并作为 api 变量发送

发布于 2024-11-25 02:03:05 字数 1417 浏览 1 评论 0原文

我需要编写一段代码,上传 doc 文件并将其内容以二进制或 ASCII 作为 API 变量发送。我可以想到两种方法(1)上传文件,然后读取其内容并将数据发送到 api(2)上传文件,然后存储其内容并访问 blob 数据并发送到 api。

我想问一下哪一个比较好。现在我正在使用第二个选项。

现在我的问题是我能够上传部分并将其作为 blob 数据存储到数据库部分, 但是当我使用 mysql 查询访问 blob 数据并将结果发送到 API 时。 API 表示它不是二进制或 ASCII 格式。当我回显查询结果时,它显示乱码数据,所以我假设它不是二进制或 ascii 格式。请告诉我如何将此 blob 数据作为 Binary/ASCII 传递给 API。

<?php

$filename = "resume3.doc";
$handle = fopen($filename, "rb");
$contents = fread($handle, filesize($filename));
fclose($handle);

// URL of Form
$url = "http://rezscore.com/a/2901e8/grade";
//create the final string to be posted
$post_string = "resume=$contents";

//create cURL connection
$curl_connection = curl_init($url);

//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 6.0;   Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);

//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

//perform our request
$result = curl_exec($curl_connection);

print $result;

//close the connection
curl_close($curl_connection);      
?>

该文件应该是简历,并且 api 返回简历的成绩。 http://rezscore.com/

谢谢

I need to write a code which uploads doc file and send its content in binary or ASCII as a API variable .I can think of two approach (1)Upload the file and then read its content and send the data to api (2)Upload the file and then store its content and access the blob data and send to api.

I would like to ask which one is better. Right now i am using second option.

Now my problem is I am able to do upload part and storing to database part as a blob data ,
but when i access the blob data using mysql query and send result to API . The API says that its not in binary or ASCII . And when i echo the result of query it shows jibberish data so i assume that its not in binary or ascii . Please tell me how to pass this blob data to API as Binary/ASCII.

<?php

$filename = "resume3.doc";
$handle = fopen($filename, "rb");
$contents = fread($handle, filesize($filename));
fclose($handle);

// URL of Form
$url = "http://rezscore.com/a/2901e8/grade";
//create the final string to be posted
$post_string = "resume=$contents";

//create cURL connection
$curl_connection = curl_init($url);

//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 6.0;   Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);

//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

//perform our request
$result = curl_exec($curl_connection);

print $result;

//close the connection
curl_close($curl_connection);      
?>

The file should be a resume and the api returns grade for the resume. http://rezscore.com/

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱殇璃 2024-12-02 02:03:05

你可以直接使用而不需要MySQL。只需将文件上传到某个临时目录并使用 http://php.net/fread 即可二进制安全地读取其内容。

You could use direct way without MySQL. Just upload the file to some temporary directory and use http://php.net/fread to binary-safe read its content.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文