PHP cURL POSTFIELDS到RESTful服务

时间:2012-09-20 16:22:14

标签: java php xml web-services rest

我正在开发一个项目,我对如何使用php curl将数据发布到java restful webservice有一些困难。

SQL:

DROP DATABASE IF EXISTS POST_TESTE;
CREATE DATABASE POST_TESTE;
USE POST_TESTE;

CREATE TABLE GRAU  (
ID_GRAU             TINYINT         UNSIGNED        NOT NULL,
DESCRICAO_GRAU      VARCHAR(20)     NOT NULL
);

ALTER TABLE GRAU ADD CONSTRAINT PK_GRAU PRIMARY KEY (ID_GRAU);
ALTER TABLE GRAU CHANGE ID_GRAU ID_GRAU TINYINT UNSIGNED NOT NULL AUTO_INCREMENT;

INSERT INTO GRAU (DESCRICAO_GRAU) VALUES ('CET');
INSERT INTO GRAU (DESCRICAO_GRAU) VALUES ('Licenciatura');
INSERT INTO GRAU (DESCRICAO_GRAU) VALUES ('Pós-Licenciatura');
INSERT INTO GRAU (DESCRICAO_GRAU) VALUES ('Pós-Graduação');
INSERT INTO GRAU (DESCRICAO_GRAU) VALUES ('Mestrado');
INSERT INTO GRAU (DESCRICAO_GRAU) VALUES ('Doutoramento');

在Netbeans中,刚刚创建了一个名为“Teste”的Web应用程序,然后从数据库创建了restful webservices,我选择了上面创建的表。创建了2个包,第1个(实体)是与表变量相关的类,第2个(服务)与提供的服务相关。

所以我们的消费服务网址是http:// localhost:8080 / Teste / resources / entity.grau

Grau.java:

@Entity
@Table(name = "grau")
@XmlRootElement
@XmlType(propOrder={"idGrau", "descricaoGrau"})
@NamedQueries({
    @NamedQuery(name = "Grau.findAll", query = "SELECT g FROM Grau g"),
    @NamedQuery(name = "Grau.findByIdGrau", query = "SELECT g FROM Grau g WHERE g.idGrau = :idGrau"),
    @NamedQuery(name = "Grau.findByDescricaoGrau", query = "SELECT g FROM Grau g WHERE g.descricaoGrau = :descricaoGrau")})
public class Grau implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @NotNull
    @Column(name = "ID_GRAU")
    private Short idGrau;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 20)
    @Column(name = "DESCRICAO_GRAU")
    private String descricaoGrau;

GrauFacadeREST.java:

@Stateless
@Path("entity.grau")
public class GrauFacadeREST extends AbstractFacade<Grau> {

@PersistenceContext(unitName = "TestePU")
private EntityManager em;

public GrauFacadeREST() {
    super(Grau.class);
}

@POST
@Override
@Consumes({"application/xml", "application/json"})
public void create(Grau entity) {
    super.create(entity);
}

@PUT
@Override
@Consumes({"application/xml", "application/json"})
public void edit(Grau entity) {
    super.edit(entity);
}

开发了更多服务,比如GET,DELETE服务,但我只想了解这两项。

我已经开发了一个带有一个输入文本的基本html,它将数据发布到.php,该.php目标是捕获信息并将其发布到例如 create 方法,在桌子上添加一个新的grau。

teste.html:

<html><body>
<form action="teste.php" method="post">
        <input type="text" name="text1" /></br>
    <input type="submit" value="Submeter" />
</form>
</body></html>

teste.php:

<?php

$var=$_POST['text1'];

$xml = new SimpleXMLElement('<graus/>');
$teste = $xml->addChild('grau');
$teste -> addChild('idGrau', '7');
$teste -> addChild('descricaoGrau', $var);
$teste1 = $xml->addChild('grau');
$teste1 -> addChild('idGrau', '8');
$teste1 -> addChild('descricaoGrau', $var);

//$xml="<graus><grau><idgrau>7</idgrau><descricaograu>asdf</descricaograu></grau><grau><idgrau>8</idgrau><descricaograu>asdf</descricaograu></grau></graus>";

/* $xml = array(
        "idgrau" => '7',
        "descricaograu" => $var,
);
*/

print($xml->asXML());

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:8080/Teste/resources/entity.grau/');
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$data=curl_exec($ch); 

if (curl_errno($ch)){
    echo curl_error($ch);
    exit;
}   
var_dump($data);
curl_close($ch);

?>

我尝试过以不同的方式更改发送后场,在后场注入阵列等等,仍然说同样的错误:

HTTP状态400 - 错误请求 客户端发送的请求在语法上是不正确的(错误请求)。

当用户使用findAll()方法时,这是通过.php开发的响应式xml。

<graus>
    <grau>
        <idGrau>1</idGrau>
        <descricaoGrau>CET</descricaoGrau>
    </grau>
    <grau>
        <idGrau>2</idGrau>
        <descricaoGrau>Licenciatura</descricaoGrau>
    </grau>
    <grau>
        <idGrau>3</idGrau>
        <descricaoGrau>Pós-Licenciatura</descricaoGrau>
    </grau>
    <grau>
        <idGrau>4</idGrau>
        <descricaoGrau>Pós-Graduação</descricaoGrau>
    </grau>
    <grau>
        <idGrau>5</idGrau>
        <descricaoGrau>Mestrado</descricaoGrau>
    </grau>
    <grau>
        <idGrau>6</idGrau>
        <descricaoGrau>Doutoramento</descricaoGrau>
    </grau>
</graus>

如果有人能提供帮助我会感激,因为我已经失去了很多时间尝试这个并且仍然不明白该怎么做。

0 个答案:

没有答案