我在 android 应用程序中有一个监听器来检测服务器 php 发送的消息
public class Threa implements Runnable
{
public static final String SERVERIP = "192.168.1.4";
public static final int SERVERPORT =6060 ; //4444
public BufferedReader in;
public int x=0;
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(SERVERPORT);
while (true)
{
x++;
Socket client = serverSocket.accept();
try {
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String str = in.readLine();
} catch(Exception e) {
} finally {
client.close();
}
}
} catch (Exception e) {
}
}
}
我在网上找到这段代码
function get_url($url)
{
$ch = curl_init();
if($ch === false)
{
die('Failed to create curl object');
}
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo get_url('http://www.apple.com/');
在 php 中使用 cURL 发送消息是正确的,如果没有解决这个问题的解决方案是什么,请给我一个想法
最佳答案
curl
适合使用 http/ftp 等协议(protocol)进行通信。
如果你想发送随机数据(即:使用你自己的协议(protocol)),那么你应该使用专门为此制作的 php 套接字: http://php.net/manual/fr/book.sockets.php
关于php - 如何在php中使用 'Curl'向android发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16013932/