我需要发送get命令并获取结果。抱歉我的英语不好 我需要从文件中获取结果url sended url参数 例如:
<?php
$adata["command1"] = "testcommand1";
$adata["command2"] = "testcommand2";
$getresult = sendGetCommand("https://website.com/api.html", $arrayofdata);
echo "["; // for json data;
$arrayresult = explode("\n",$getresult);
foreach ($getresult in $line) {
$arrayline = explode("\n",$line);
echo "{ ";
foreach ($arrayline in $cmdid => $cmd) {
echo "'".$cmdid."' : '".$cmd."',";
}
echo "{";
}
?>
有点想这样..
网址如下:
“https://website.com/api.html?command1=testcommand1&command2=testcommand2”
url结果如下:
命令1,testcommand1,是
command2,testcommand2,错误,错误文本在这里
我将逐行分解数据,然后从JavaScript获取数据 这是一个域名搜索API。
另一个问题: explode(“\ n”,$ string)可以用来逐行读取吗? (windows os)
答案 0 :(得分:4)
$url = "https://website.com/api.html?command1=".$adata["command1"]."&command2=".$adata["command2"];
$getresult = file_get_contents($url);
祝你好运;
答案 1 :(得分:2)
要阅读结果,您应该查看str_getcsv和/或fgetcsv,而不是使用爆炸手动执行此操作。
编辑:要发送获取请求,您应该查看fsockopen及其示例。你可以使用像this这样的函数(只需将POST更改为GET,内容类型就像你需要的那样):
function _get($type,$host,$port='80',$path='/',$data='') {
$_err = 'lib sockets::'.__FUNCTION__.'(): ';
switch($type) { case 'http': $type = ''; case 'ssl': continue; default: die($_err.'bad $type'); } if(!ctype_digit($port)) die($_err.'bad port');
if(!empty($data)) foreach($data AS $k => $v) $str .= urlencode($k).'='.urlencode($v).'&'; $str = substr($str,0,-1);
$fp = fsockopen($host,$port,$errno,$errstr,$timeout=30);
if(!$fp) die($_err.$errstr.$errno); else {
fputs($fp, "POST $path HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
fputs($fp, "Content-length: ".strlen($str)."\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $str."\r\n\r\n");
while(!feof($fp)) $d .= fgets($fp,4096);
fclose($fp);
} return $d;
}