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


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

以下是步骤:

  • POST表单(文件/图像输入)从浏览器转到PHP服务器
  • 然后,PHP服务器使用fsockopen到Java(GlassFish/Jersy)REST服务,将图像作为内容发送,以便在那里完成更高级的成像工作,并将生成的图像返回给PHP服务器(或仅返回图像的URI)
  • 然后,PHP服务器在HTML文档中将结果(img src=..)回显给用户

在第一步中获取图像及其所有属性非常有效,但我需要帮助在从PHP到web服务的POST请求中正确设置头部。

当前代码:

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

这会返回到浏览器:"HTTP/1.1 400错误请求内容类型:text/html内容长度:717"。

因此,我需要格式更好的头来使用RESTWeb方法。如果我能做到这一点,有人知道在球衣网方法中使用什么参数来访问图像吗?对于标准HTML表单,它是这样的:

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

很乐意为在HTML中实现这一点提供更好的架构建议。

干杯

您可能对写得不好的请求有问题,应该首先使用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);

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

以及通过curl_getinfo():处理HTTP状态代码

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

您也可以手动设置http头:

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