【发布时间】:2014-07-24 19:52:35
【问题描述】:
我有这个使用 cURL 解析来自 Indeed.com 的 xml 提要的 php 代码。我正在将 REMOTE_ADDR 和 HTTP_USER_AGENT 等服务器信息传递到 url 的参数中,但它们没有被传递。
检查下面这部分代码。 '.geoCheckIP($_SERVER['REMOTE_ADDR']).'
这是正确的做法。只是不确定当它是 CURLOPT_URL =>
中数组的一部分时它是否是正确的方法当在 cURL 中使用数组时,将这些服务器 sn-ps 传递给 url 参数的正确方法是什么,例如 CURLOPT_URL => 中的以下函数?
下面的 php 代码是我页面上的完整代码,因此您可以更好地了解发生了什么。
我正在尝试检测用户到达我的网站时所在的城市和州,以显示他们当地的工作列表。 php 代码有效,我可以在网页上回显城市状态,但它只是没有将相同的信息传递给数组中的函数 curl_request()。请帮忙。
<?php
// Convert IP into city state country (Geo Location function)
function geoCheckIP($ip){
if(!filter_var($ip, FILTER_VALIDATE_IP))
{
throw new InvalidArgumentException("IP is not valid");
}
$response=@file_get_contents('http://www.netip.de/search?query='.$ip);
if (empty($response))
{
throw new InvalidArgumentException("Error contacting Geo-IP-Server");
}
$patterns=array();
$patterns["domain"] = '#Domain: (.*?) #i';
$patterns["country"] = '#Country: (.*?) #i';
$patterns["state"] = '#State/Region: (.*?)<br#i';
$patterns["town"] = '#City: (.*?)<br#i';
$ipInfo=array();
foreach ($patterns as $key => $pattern)
{
$ipInfo[$key] = preg_match($pattern,$response,$value) && !empty($value[1]) ? $value[1] : 'not found';
}
/*I've included the substr function for Country to exclude the abbreviation (UK, US, etc..)
To use the country abbreviation, simply modify the substr statement to:
substr($ipInfo["country"], 0, 3)
*/
$ipdata = $ipInfo["town"]. ", ".$ipInfo["state"]/*.", ".substr($ipInfo["country"], 4)*/;
return $ipdata;
}
// Indeed php function
function curl_request(){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://api.indeed.com/ads/apisearch?publisher=&q=computer+repair&l='.geoCheckIP($_SERVER['REMOTE_ADDR']).'&sort=&radius=25&st=&jt=&start=&limit=&fromage=&highlight=1&filter=1&latlong=0&co=us&chnl=computer+help+wanted&userip='.$_SERVER['REMOTE_ADDR'].'&useragent='.$_SERVER['HTTP_USER_AGENT'].'&v=2',
));
$resp = curl_exec($curl);
curl_close($curl);
return $resp;
}
function xmlToArray($input, $callback = null, $recurse = false) {
$data = ((!$recurse) && is_string($input))? simplexml_load_string($input, 'SimpleXMLElement', LIBXML_NOCDATA): $input;
if ($data instanceof SimpleXMLElement) $data = (array) $data;
if (is_array($data)) foreach ($data as &$item) $item = xmlToArray($item, $callback, true);
return (!is_array($data) && is_callable($callback))? call_user_func($callback, $data): $data;
}
?>
函数调用
这是函数在正文中的调用方式
<ol>
<?php
for($i=0;$i<10;$i++){ // using for loop to show number of jobs
$resp=curl_request($i);
$arrXml = xmlToArray($resp);
$results=$arrXml['results'];
?>
<li>
<p><strong>Job :</strong> <a href="<?php echo $results['result'][$i]['url']; ?>" target="_blank"><?php echo $results['result'][$i]['jobtitle']; ?></a></p>
<p><strong>Company:</strong> <?php echo $results['result'][$i]['company']; ?></p>
<p><strong>Location:</strong> <?php echo $results['result'][$i]['formattedLocationFull']; ?></p>
<p><strong>Date Posted :</strong> <?php echo $results['result'][$i]['formattedRelativeTime'];?> on <?php echo $results['result'][$i]['date'];?></p>
<p><strong>Description :</strong> <?php echo $results['result'][$i]['snippet']; ?></p>
</li>
<?php } ?>
</ol>
什么有效
仅当我从 CURLOPT_URL 中删除变量时,上面的代码才有效
这行得通
CURLOPT_URL => 'http://api.indeed.com/ads/apisearch?publisher=&q=computer+repair&l=city,state&sort=&radius=25&st=&jt=&start=&limit=&fromage=&highlight=1&filter=1&latlong=0&co=us&chnl=computer+help+wanted&userip=111.111.111.111&useragent=mozila&v=2',
这不起作用
CURLOPT_URL => 'http://api.indeed.com/ads/apisearch?publisher=&q=computer+repair&l='.geoCheckIP($_SERVER['REMOTE_ADDR']).'&sort=&radius=25&st=&jt=&start=&limit=&fromage=&highlight=1&filter=1&latlong=0&co=us&chnl=computer+help+wanted&userip='.$_SERVER['REMOTE_ADDR'].'&useragent='.$_SERVER['HTTP_USER_AGENT'].'&v=2',
【问题讨论】:
-
把网址的
geoCheckIP()部分改成静态文本是否有效?? -
@Novocaine88 是的。当我将此部分
'.geoCheckIP($_SERVER['REMOTE_ADDR']).'更改为new york, ny之类的静态文本或静态邮政编码也可以10001时,它可以工作。你还能帮忙吗? -
我唯一的建议是您的
geoCheckIP功能可能无法正常工作。如果您自己使用它,它是否会按预期返回一个字符串?我怀疑它没有,考虑到使用静态文本切换该功能。 -
@Novocaine88 是的,geocheckip 工作正常。毕竟我没有使用这个代码。我现在正在使用一种没有 curl 的更好的代码。 curl 代码有问题,加载速度也很慢。
标签: php arrays variables curl url-parameters