简体   繁体   English

从PHP POST到另一个(Java)Web服务并检索(图像)结果

[英]POST from PHP to another (Java) web service and retrieving the (image) result

These are the steps: 这些步骤是:

  • POST Form (file/image input) from the browser goes to a PHP server. 来自浏览器的POST表单(文件/图像输入)进入PHP服务器。
  • The PHP server then use fsockopen to a Java (GlassFish/Jersey) REST service, sending the image as content for more advanced imaging work to be done there, and returning the resulting image back to the PHP server (or returning only a URI to the image). 然后,PHP服务器使用fsockopen到Java(GlassFish / Jersey)REST服务,将图像作为内容发送到该处进行更高级的成像工作,然后将结果图像返回给PHP服务器(或仅将URI返回给图片)。
  • The PHP server then echos the result (img src= ..) back to the user in the HTML document. 然后,PHP服务器将结果(img src = ..)回显到HTML文档中的用户。

Getting the image and all its attributes in the first step works great, but I need help setting up the headers correctly in the POST request from PHP to the web service. 在第一步中获取图像及其所有属性非常有用,但是我需要帮助来正确设置从PHP到Web服务的POST请求中的标头。

Current code: 当前代码:

$fp = fsockopen("domain..", 80, $errno, $errstr, 30);
$contentlength = $_FILES["photo_file"]["size"];
$imageref = $_FILES["photo_file"]["tmp_name"];

$out = "POST /Uri to resource .. HTTP/1.1\r\n";
$out .= "Host: http://... \r\n";
$out .= "Connection: Keep-Alive\r\n";
$out .= "Content-type: multipart/mixed\r\n";
$out .= "Content-length: $contentlength\r\n\r\n";
$out .= "$imageref";

fwrite($fp, $out);
$theOutput;
while (!feof($fp))
{
  $theOutput .= fgets($fp, 128);
}
echo $theOutput;
fclose($fp);

Which echoes to the browser: "HTTP/1.1 400 Bad Request Content-Type: text/html Content-Length: 717". 它在浏览器中回显:“ HTTP / 1.1 400错误的请求内容类型:text / html内容长度:717”。

So I need better formed headers to get through to the REST web-method. 因此,我需要格式更好的标头才能进入REST网络方法。 And if I should achieve that does anyone know what paramaters to use in the jersey web-method to access the image? 如果我应该实现这一目标,那么谁知道在jersey网络方法中使用哪些参数来访问图像? For standard HTML forms its this: 对于标准HTML格式,它是这样的:

@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response getFullImage(@FormDataParam("photo_file") InputStream imageIS {..ImageIO.read(imageIS)..}

Would love suggestions to better architecture also for achieving this in HTML. 也希望为HTML实现这一点提供更好的体系结构的建议。

Cheers 干杯

You probably have problem with badly written request and should use cURL at first place (in php), basic example usage (with writing to the file) from manual page : 您可能遇到了错误编写的请求的问题,应该首先从手册页中使用cURL (在php中),基本示例用法(与写入文件):

$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);

Another example for setting post data from curl_setopt() page : curl_setopt()页面设置发布数据的另一个示例:

$data = array('name' => 'Foo', 'file' => '@/home/user/test.png');

curl_setopt($ch, CURLOPT_URL, 'http://localhost/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

And handling HTTP status codes via curl_getinfo() : 并通过curl_getinfo()处理HTTP状态代码:

if(curl_getinfo($c, CURLINFO_HTTP_CODE) === 200){
    ...
}

You also may set http headers manually: 您也可以手动设置http标头:

curl_setopt($cURL,CURLOPT_HTTPHEADER,array (
        "Content-Type: text/xml; charset=utf-8",
        "Expect: 100-continue"
    ));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM