通过CURL接收到的信息的内容为:login = Vasya Pupkin city = Moscow tel = 0 123456567 567 sex = male
如何正确地将其分成数组以进一步处理数据?目前,我已经有了这段代码,所以为什么不能跳过它呢:forach()提供了无效的参数,我理解了这一点,因为接收到的信息没有传输到数组中。
<?php
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, "Justice.ru");
$data = iconv('windows-1251', 'UTF-8', $data);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$lines = file_get_contents_curl('http://emeraldscity.combats.ru/inf.pl?short=1327641470');
foreach($lines as $value)
{
list($var, $val) = explode('=',$value);
$arr[$var] = $val;
}
echo $arr['login'];
?>
答案 0 :(得分:0)
<?php
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, "Justice.ru");
$data = curl_exec($ch);
curl_close($ch);
$d = json_encode($data);
return json_decode($d , true);
}
$lines = file_get_contents_curl('http://emeraldscity.combats.ru/inf.pl?short=1327641470');
foreach($lines as $value)
{
list($var, $val) = explode('=',$value);
$arr[$var] = $val;
}
echo $arr[login];
?>
答案 1 :(得分:0)
这个坚强的人必须具有某种风格,例如xml或json或数组..
答案 2 :(得分:0)
使用file_get_contents_curl,您将得到一个字符串,而不是一个数组。因此,在浏览各行之前,您必须从字符串中提取它们。
$content = file_get_contents_curl('http://emeraldscity.combats.ru/inf.pl?short=1327641470');
$lines = explode(PHP_EOL,$content);
foreach ($lines as $value) {
list($var, $val) = explode('=', $value);
$arr[$var] = $val;
}