做一些PHP机试题

注:所有题目来自网络

第一道

将第三方api的前3条数据全部读取出来,存入对应的数据库字段
api URL:http://pub.cloudmob.mobi/publisherapi/offers/?uid=92&key=d4bab08884781dbf2bede528e27d243d&limit=1&page=1

$curl = curl_init();
$api = 'http://pub.cloudmob.mobi/publisherapi/offers/?uid=92&key=d4bab08884781dbf2bede528e27d243d&limit=1&page=';
$mysql = new PDO("mysql:host=127.0.0.1;dbname=test;charset=utf8", "root", "root123");

for ($i=1; $i <= 3; $i++) { 
	curl_setopt($curl, CURLOPT_URL, $api . $i); // 设定要请求的URL 
	curl_setopt($curl, CURLOPT_HEADER, 0); // 不接收header头信息
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 收到的内容不直接输出

	$data = curl_exec($curl); // 使用curl组件执行请求
	$data = json_decode($data, true);

	foreach ($data['data'] as $key => $value) {
		$sql = "insert into test(oid,avail,name)values(?,?,?)";
		$tpl = $mysql->prepare($sql);
		$tpl->bindValue(1, $value['oid']);
		$tpl->bindValue(2, $value['avail']);
		$tpl->bindValue(3, $value['name']);
		$tpl->execute();
		// echo PHP_EOL;
	}
}

$mysql = null;
curl_close($curl);

这块的话,只是一个简单的实现,优化的话可以指定limit生成多次请求并行处理,也就是说需要用到rpc并行调度器,可以使用这个库https://github.com/ybc429710096/scheduler

第二道

TODO.

发布了116 篇原创文章 · 获赞 12 · 访问量 99万+

猜你喜欢

转载自blog.csdn.net/u012628581/article/details/102921101