Php cUrl_setopt到Java

时间:2015-10-27 11:17:47

标签: java php

我有一项任务,我需要实现从PHP到java编程语言的post请求。这是代码段,java中的等价物是什么?我不知道curl_setopt部分

function send()
{
    $fieldcnt    = 6;

    $data = array(
        'UserID' => $this->username,
        'PWD' => $this->password,
        'APIKEY' => $this->apikey,
        'FROM' => $this->from,
        'TO' => $this->to,
        'MSG' => $this->msgtext
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $this->base_url_SendSMS);
    curl_setopt($ch, CURLOPT_POST, $fieldcnt);
    curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $res = curl_exec($ch);
    curl_close($ch);
    return $res;
}

提前致谢!

2 个答案:

答案 0 :(得分:1)

好,所以首先您需要有一个数据类

 public class Data {
            private String UserID;
            private String PWD;
            private String APIKEY;
            private String FROM;
            private String TO;
            private String MSG;
    }

然后为每个班级成员生成getter / setters

public String getUserID() {
        return UserID;
    }

    public void setUserID(String userID) {
        UserID = userID;
    }

    public String getPWD() {
        return PWD;
    }

    public void setPWD(String pWD) {
        PWD = pWD;
    }

    public String getAPIKEY() {
        return APIKEY;
    }

    public void setAPIKEY(String aPIKEY) {
        APIKEY = aPIKEY;
    }

    public String getFROM() {
        return FROM;
    }

    public void setFROM(String fROM) {
        FROM = fROM;
    }

    public String getTO() {
        return TO;
    }

    public void setTO(String tO) {
        TO = tO;
    }

    public String getMSG() {
        return MSG;
    }

    public void setMSG(String mSG) {
        MSG = mSG;
    }

此时,您已准备好转换您的php代码

$data = array(
        'UserID' => $this->username,
        'PWD' => $this->password,
        'APIKEY' => $this->apikey,
        'FROM' => $this->from,
        'TO' => $this->to,
        'MSG' => $this->msgtext
    );

到JAVA。

让我们创建您的http_build_query

Data data = new Data();
        
        data.setUserID("ASDFV238756EW");
        data.setPWD(role);
        data.setAPIKEY("<Your Key>");

现在您需要为数据类创建JSON

JSONObject json = new JSONObject(data);

要在Java中使用curl,您需要使用Process Builder程序包

注意:如果使用Windows,则可以保留curl.exe,但是如果使用Linux,则必须将curl.exe更改为/ usr / bin / curl

ProcessBuilder process = new ProcessBuilder("curl.exe", "[[Your-SMS-Url-Here]]",
                "-X","POST",
                "-H", "Authorization:Bearer <Your Access Token if provided>", 
                "-H", "headers1",
                "-H","headers2",
                "-d",json.toString());

所以现在您都可以使用curl调用了,只需要添加一个进程即可。

Process p;
        try {
            p = process.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            StringBuilder builder = new StringBuilder();
            String line = null;

            while ((line = reader.readLine()) != null) {
                builder.append(line);
                builder.append(System.getProperty("line.separator"));
            }
            /*Set your debug point here, you will see the api response in x vairable below*/
            String x = builder.toString();
            
            try
            {
                
            }
            catch(Exception e)
            {
                if(customer.getDeleted() == true)
                {
                }
                else
                {
                }
                
            }
            
        } catch (IOException e) {
            System.out.print("error");
            e.printStackTrace();
        }

答案 1 :(得分:0)

在java中,您可能希望使用像HttpClient

这样的库