Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将java rest请求转换为php代码_Java_Php_Rest_Curl - Fatal编程技术网

将java rest请求转换为php代码

将java rest请求转换为php代码,java,php,rest,curl,Java,Php,Rest,Curl,如何将此java代码转换为php server = new URL(url); HttpURLConnection urlConnection = (HttpURLConnection) server.openConnection() ; urlConnection.setRequestMethod( method ); urlConnection.setDoInput(true); urlConnection.setDoOutput(true); ur

如何将此java代码转换为php

server = new URL(url);
    HttpURLConnection urlConnection  = (HttpURLConnection) server.openConnection() ;
    urlConnection.setRequestMethod( method );
    urlConnection.setDoInput(true);
    urlConnection.setDoOutput(true);
    urlConnection.setRequestProperty("Content-Type",mimeType );
    if( requestParameter != null)
    {
        CyberoamLogger.appLog.debug(MODULE+":"+METHOD+"Request parameter: "+requestParameter.toString());
        out = new BufferedWriter( new OutputStreamWriter( urlConnection.getOutputStream() ) );
        out.write(requestParameter.toString());
        out.flush();
        out.close() ;
    }

    urlConnection.connect();    
    strbuf = new StringBuffer();            
     is= urlConnection.getInputStream()   ;   
    byte[] buffer = new byte[1024 * 4];        
    int n = 0;
    while (-1 != (n = is.read(buffer)))
        strbuf.append(new String(buffer, 0, n));
    is.close();
    strResponse=strbuf.toString();
    CyberoamLogger.appLog.debug(MODULE+METHOD+ " WS Responce in String  : "+strResponse);
    urlConnection.disconnect();
当我尝试遵循php代码时,我发现服务器拒绝了这个请求,因为请求实体的格式不受请求方法()的请求资源的支持。错误


尝试使用卷曲设置内容类型标题:

$mimeType = "application/json";
curl_setopt($curl, CURLOPT_HTTPHEADER,array("Content-Type: $mimeType"));
// equivalent to your java urlConnection.setRequestProperty("Content-Type",mimeType );
下面是一个准备json的示例:

$data = array(
    'name' => 'alex',
    'value' => '100'
);
$json_data = json_encode($data); // {"name":"alex","value":"100"}

mimeType是一个包含“application/json”的java变量。在php中,当我放入CURLOPT_HTTPHEADER数组(“内容类型:application/json”);客户端发送的请求语法错误()。@siddhartnagar您的json数据格式可能不正确。检查如何使用我更新的答案中正确格式的json。
$data = array(
    'name' => 'alex',
    'value' => '100'
);
$json_data = json_encode($data); // {"name":"alex","value":"100"}