php 请求java_怎么php发送get请求给Java,然后返回想要的具体参数

展开全部

curl请求java接口,接口返回值后进行相关操作,给你贴一个e69da5e6ba903231313335323631343130323136353331333361313234curl的代码function ihttp_request($url, $post = '', $extra = array(), $timeout = 60) {

$urlset = parse_url($url);

if (empty($urlset['path'])) {

$urlset['path'] = '/';

}

if (!empty($urlset['query'])) {

$urlset['query'] = "?{$urlset['query']}";

}

if (empty($urlset['port'])) {

$urlset['port'] = $urlset['scheme'] == 'https' ? '443' : '80';

}

if (strexists($url, 'https://') && !extension_loaded('openssl')) {

if (!extension_loaded("openssl")) {

message('请开启您PHP环境的openssl');

}

}

if (function_exists('curl_init') && function_exists('curl_exec')) {

$ch = curl_init();

if (ver_compare(phpversion(), '5.6') >= 0) {

curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);

}

if (!empty($extra['ip'])) {

$extra['Host'] = $urlset['host'];

$urlset['host'] = $extra['ip'];

unset($extra['ip']);

}

curl_setopt($ch, CURLOPT_URL, $urlset['scheme'] . '://' . $urlset['host'] . ($urlset['port'] == '80' ? '' : ':' . $urlset['port']) . $urlset['path'] . $urlset['query']);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch, CURLOPT_HEADER, 1);

@curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);

if ($post) {

if (is_array($post)) {

$filepost = false;

foreach ($post as $name => $value) {

if ((is_string($value) && substr($value, 0, 1) == '@') || (class_exists('CURLFile') && $value instanceof CURLFile)) {

$filepost = true;

break;

}

}

if (!$filepost) {

$post = http_build_query($post);

}

}

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

}

if (!empty($GLOBALS['_W']['config']['setting']['proxy'])) {

$urls = parse_url($GLOBALS['_W']['config']['setting']['proxy']['host']);

if (!empty($urls['host'])) {

curl_setopt($ch, CURLOPT_PROXY, "{$urls['host']}:{$urls['port']}");

$proxytype = 'CURLPROXY_' . strtoupper($urls['scheme']);

if (!empty($urls['scheme']) && defined($proxytype)) {

curl_setopt($ch, CURLOPT_PROXYTYPE, constant($proxytype));

} else {

curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);

curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);

}

if (!empty($GLOBALS['_W']['config']['setting']['proxy']['auth'])) {

curl_setopt($ch, CURLOPT_PROXYUSERPWD, $GLOBALS['_W']['config']['setting']['proxy']['auth']);

}

}

}

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

curl_setopt($ch, CURLOPT_SSLVERSION, 1);

if (defined('CURL_SSLVERSION_TLSv1')) {

curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);

}

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1');

if (!empty($extra) && is_array($extra)) {

$headers = array();

foreach ($extra as $opt => $value) {

if (strexists($opt, 'CURLOPT_')) {

curl_setopt($ch, constant($opt), $value);

} elseif (is_numeric($opt)) {

curl_setopt($ch, $opt, $value);

} else {

$headers[] = "{$opt}: {$value}";

}

}

if (!empty($headers)) {

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

}

}

$data = curl_exec($ch);

$status = curl_getinfo($ch);

$errno = curl_errno($ch);

$error = curl_error($ch);

curl_close($ch);

if ($errno || empty($data)) {

return error(1, $error);

} else {

return ihttp_response_parse($data);

}

}

$method = empty($post) ? 'GET' : 'POST';

$fdata = "{$method} {$urlset['path']}{$urlset['query']} HTTP/1.1\r\n";

$fdata .= "Host: {$urlset['host']}\r\n";

if (function_exists('gzdecode')) {

$fdata .= "Accept-Encoding: gzip, deflate\r\n";

}

$fdata .= "Connection: close\r\n";

if (!empty($extra) && is_array($extra)) {

foreach ($extra as $opt => $value) {

if (!strexists($opt, 'CURLOPT_')) {

$fdata .= "{$opt}: {$value}\r\n";

}

}

}

$body = '';

if ($post) {

if (is_array($post)) {

$body = http_build_query($post);

} else {

$body = urlencode($post);

}

$fdata .= 'Content-Length: ' . strlen($body) . "\r\n\r\n{$body}";

} else {

$fdata .= "\r\n";

}

if ($urlset['scheme'] == 'https') {

$fp = fsockopen('ssl://' . $urlset['host'], $urlset['port'], $errno, $error);

} else {

$fp = fsockopen($urlset['host'], $urlset['port'], $errno, $error);

}

stream_set_blocking($fp, true);

stream_set_timeout($fp, $timeout);

if (!$fp) {

return error(1, $error);

} else {

fwrite($fp, $fdata);

$content = '';

while (!feof($fp))

$content .= fgets($fp, 512);

fclose($fp);

return ihttp_response_parse($content, true);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您想知道如何使用PHP接收JSON POST数据,下面是一个简单的示例代码: ```php <?php // 获取POST请求中的JSON数据 $json = file_get_contents('php://input'); // 将JSON数据转换为PHP数组 $data = json_decode($json, true); // 打印PHP数组 print_r($data); ?> ``` 在上面的代码中,我们首先使用`file_get_contents()`函数从`php://input`中获取POST请求中的JSON数据,然后使用`json_decode()`函数将JSON数据转换为PHP数组,最后使用`print_r()`函数打印PHP数组。 如果您想要接收到POST请求返回JSON数据,可以使用如下代码: ```php <?php // 获取POST请求中的JSON数据 $json = file_get_contents('php://input'); // 将JSON数据转换为PHP数组 $data = json_decode($json, true); // 创建一个包含返回数据PHP数组 $response = array( 'status' => 'success', 'message' => 'Received JSON data successfully!', 'data' => $data ); // 将PHP数组转换为JSON字符串 $json_response = json_encode($response); // 设置HTTP响应头 header('Content-Type: application/json'); // 输出JSON字符串 echo $json_response; ?> ``` 在上面的代码中,我们首先使用`file_get_contents()`函数从`php://input`中获取POST请求中的JSON数据,然后使用`json_decode()`函数将JSON数据转换为PHP数组,接着创建一个包含返回数据PHP数组,然后使用`json_encode()`函数将PHP数组转换为JSON字符串,最后设置HTTP响应头并输出JSON字符串。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值