gpt4 book ai didi

Java URLConnection 到 php

转载 作者:行者123 更新时间:2023-11-30 11:53:25 25 4
gpt4 key购买 nike

如何在 php 中编写这段代码?

我应该使用什么? curl ?开放?以及实际发送到服务器的是什么(outputString 是一个 post/get 及其变量名称)?

URL url = new URL(targetURL);
final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","text/xml");
conn.setDoOutput(true);

OutputStream out = conn.getOutputStream();
out.write(outputString.getBytes("UTF-8"));
out.close();

conn.connect();

final int code = conn.getResponseCode();
final String contentType = conn.getContentType();
final StringBuffer responseText = new StringBuffer();
InputStreamReader in = new InputStreamReader(conn.getInputStream(),"UTF-8");

char[] msg = new char[2048];
int len;
while ((len = in.read(msg)) > 0) {
responseText.append(msg, 0, len);
}

感谢您的任何回答。

最佳答案

这是一个 cURL 帖子的基本示例...

进一步阅读 http://www.php.net/manual/en/function.curl-exec.php也有很好的例子。

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"http://www.site.com/test.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"var1=value1&var2=value2&var3=value3");

// Get server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec ($ch);

curl_close ($ch);

// further processing ....
if ($result == "OK") { ... } else { ... }

?>

发送 XML 的示例:

<?php
/**
* Define POST URL and also payload
*/
define('XML_PAYLOAD', '<?xml version="1.0"?><member><name>name</name></member>');
define('XML_POST_URL', 'http://www.domain.com/build_xml.php');

/**
* Initialize handle and set options
*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, XML_POST_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_POSTFIELDS, XML_PAYLOAD);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));

/**
* Execute the request and also time the transaction
*/
$start = array_sum(explode(' ', microtime()));
$result = curl_exec($ch);
$stop = array_sum(explode(' ', microtime()));
$totalTime = $stop - $start;

/**
* Check for errors
*/
if ( curl_errno($ch) ) {
$result = 'ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch);
} else {
$returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
switch($returnCode){
case 404:
$result = 'ERROR -> 404 Not Found';
break;
default:
break;
}
}

/**
* Close the handle
*/
curl_close($ch);

/**
* Output the results and time
*/
echo 'Total time for request: ' . $totalTime . "\n";
echo $result;

/**
* Exit the script
*/
exit(0);
?>

第三个是很好的衡量标准,只是为了说明另一种方法;

<?php
$xml = '<request>Testing</request>';
$server = '...'; // URL to server.php
$options = array
(
CURLOPT_URL => $server,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $xml,
CURLOPT_RETURNTRANSFER => true
);


$curl = curl_init();
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
curl_close($curl);

echo '<pre>', htmlspecialchars($response), '</pre>';

?>

关于Java URLConnection 到 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6276130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com