- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有这个 php 代码,它使用 cURL 来解析来自 Indeed.com 的 xml 提要。我将 REMOTE_ADDR 和 HTTP_USER_AGENT 等服务器信息传递到 url 的参数中,但它们没有被传递。
检查下面的这部分代码。 '.geoCheckIP($_SERVER['REMOTE_ADDR']).'
这是正确的做法。只是不确定当它是 CURLOPT_URL => 中数组的一部分时这样做是否是正确的方法 =>
在 cURL 中使用数组时,将这些服务器片段传递到 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',
最佳答案
为此不需要使用 cURL,而且加载速度非常慢。这是一个非常简单的方法,可以使用 geoCheckIP 函数在纯 php 中获取 xml 文档的结果。
<?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.com API URL parameters
$url = 'http://api.indeed.com/ads/apisearch'.'?';
$publisher = 'YOUR PUB NUMBER GOES HERE';
$q = 'title:(java or java+programmer or java+programming)';
$l = geoCheckIP($_SERVER['REMOTE_ADDR']);
$sort = 'date';
$radius = '20';
$st = '';
$jt = '';
$start = '0';
$limit = '25';
$fromage = '';
$highlight = '0';
$filter = '1';
$latlong = '0';
$co = 'us';
$chnl = 'YOUR CHANNEL NAME';
$userip = $_SERVER['REMOTE_ADDR'];
$useragent = isset($_SERVER['HTTP_USER_AGENT']) ? ($_SERVER['HTTP_USER_AGENT']) : 'unknown';
$v = '2';
然后您在下面看到的其余代码将位于 <body>
下方您希望输出显示的页面标记。
<!-- BEGIN INDEED ORDERED LIST-->
<ol class="jobs">
<?php
$xml = simplexml_load_file($url."publisher=".$publisher."&q=".$q."&l=".$l."&sort=".$sort."&radius=".$radius."&st=".$st."&jt=".$jt."&start=".$start."&limit=".$limit."&fromage=".$fromage."&highlight=".$highlight."&filter=".$filter."&latlong=".$latlong."&co=".$co."&chnl=".$chnl."&userip=".$userip."&useragent=".$useragent."&v=".$v);
foreach($xml->results->result as $result) { ?>
<li class="job">
<div id="jobtitle"><strong><a onmousedown="<?php echo $result->onmousedown;?>" rel="nofollow" href="<?php echo $result->url;?>" target="_blank"><?php echo $result->jobtitle;?></a></strong></div>
<div id="company"><?php echo $result->company;?></div>
<div id="snippet">
<?php echo $result->snippet;?>
</div>
<div id="location"><strong>Location:</strong> <?php echo $result->formattedLocationFull;?></div>
<div id="date"><span class="posted">Posted <?php echo $result->formattedRelativeTime;?></span></div>
</li>
<?php } ?>
</ol>
<!-- END INDEED ORDERED LIST -->
关于php - 如何将变量作为 cURL 数组中的 url 参数传递给 CURLOPT_URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24032774/
简而言之:我想从可变参数模板参数中提取各种选项,但不仅通过标签而且通过那些参数的索引,这些参数是未知的 标签。我喜欢 boost 中的方法(例如 heap 或 lockfree 策略),但想让它与 S
我可以对单元格中的 excel IF 语句提供一些帮助吗? 它在做什么? 对“BaselineAmount”进行了哪些评估? =IF(BaselineAmount, (Variance/Baselin
我正在使用以下方法: public async Task Save(Foo foo,out int param) { ....... MySqlParameter prmparamID
我正在使用 CodeGear RAD Studio IDE。 为了使用命令行参数测试我的应用程序,我多次使用了“运行 -> 参数”菜单中的“参数”字段。 但是每次我给它提供一个新值时,它都无法从“下拉
我已经为信用卡类编写了一些代码,粘贴在下面。我有一个接受上述变量的构造函数,并且正在研究一些方法将这些变量格式化为字符串,以便最终输出将类似于 号码:1234 5678 9012 3456 截止日期:
MySql IN 参数 - 在存储过程中使用时,VarChar IN 参数 val 是否需要单引号? 我已经像平常一样创建了经典 ASP 代码,但我没有更新该列。 我需要引用 VarChar 参数吗?
给出了下面的开始,但似乎不知道如何完成它。本质上,如果我调用 myTest([one, Two, Three], 2); 它应该返回元素 third。必须使用for循环来找到我的解决方案。 funct
将 1113355579999 作为参数传递时,该值在函数内部变为 959050335。 调用(main.c): printf("%d\n", FindCommonDigit(111335557999
这个问题在这里已经有了答案: Is Java "pass-by-reference" or "pass-by-value"? (92 个回答) 关闭9年前。 public class StackOve
我真的很困惑,当像 1 == scanf("%lg", &entry) 交换为 scanf("%lg", &entry) == 1 没有区别。我的实验书上说的是前者,而我觉得后者是可以理解的。 1 =
我正在尝试使用调用 SetupDiGetDeviceRegistryProperty 的函数使用德尔福 7。该调用来自示例函数 SetupEnumAvailableComPorts .它看起来像这样:
我需要在现有项目上实现一些事件的显示。我无法更改数据库结构。 在我的 Controller 中,我(从 ajax 请求)传递了一个时间戳,并且我需要显示之前的 8 个事件。因此,如果时间戳是(转换后)
rails 新手。按照多态关联的教程,我遇到了这个以在create 和destroy 中设置@client。 @client = Client.find(params[:client_id] || p
通过将 VM 参数设置为 -Xmx1024m,我能够通过 Eclipse 运行 Java 程序-Xms256M。现在我想通过 Windows 中的 .bat 文件运行相同的 Java 程序 (jar)
我有一个 Delphi DLL,它在被 Delphi 应用程序调用时工作并导出声明为的方法: Procedure ProduceOutput(request,inputs:widestring; va
浏览完文档和示例后,我还没有弄清楚 schema.yaml 文件中的参数到底用在哪里。 在此处使用 AWS 代码示例:https://github.com/aws-samples/aws-proton
程序参数: procedure get_user_profile ( i_attuid in ras_user.attuid%type, i_data_group in data_g
我有一个字符串作为参数传递给我的存储过程。 dim AgentString as String = " 'test1', 'test2', 'test3' " 我想在 IN 中使用该参数声明。 AND
这个问题已经有答案了: When should I use "this" in a class? (17 个回答) 已关闭 6 年前。 我运行了一些java代码,我看到了一些我不太明白的东西。为什么下
我输入 scroll(0,10,200,10);但是当它运行时,它会传递字符串“xxpos”或“yypos”,我确实在没有撇号的情况下尝试过,但它就是行不通。 scroll = function(xp
我是一名优秀的程序员,十分优秀!