我正在开发一个脚本,每次运行时都会将文件列表保存到文件中。
每次脚本运行时,都应该检查文件,如果文件中没有包含网址,则应将该网址发布到Facebook。如果网址已经在文件中,则不应将其发布到Facebook。
现在,每次脚本运行时,即使已经在文件中,也会将一个网址发布到Facebook。不确定我做错了什么:
<?php
define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__.'/src/Facebook/');
require_once(__DIR__.'/src/Facebook/autoload.php');
require('simple_html_dom.php');
$fb = new Facebook\Facebook([
'app_id' => 'xxxxxxxx',
'app_secret' => 'xxxxxxxx',
'default_graph_version' => 'v2.8',
]);
$html = file_get_html('https://www.trulia.com/for_sale/McKinney,TX/300000p_price/SINGLE-FAMILY_HOME_type/date;d_sort/');
$houses = [];
$i = 1;
foreach ($html->find('.cardContainer') as $house) {
if ($i > 2) {
break;
}
$houseLink = $house->find('a.tileLink', 0);
$houseUrl = 'https://trulia.com' . $houseLink->href;
file_put_contents("houses.txt", ($houseUrl . "\n"), FILE_APPEND);
$singleHouseHtml = file_get_html("{$houseUrl}");
$singleHouseHtmlDesc = $singleHouseHtml->find('span#corepropertydescription', 0);
$singleHouseHtmlDescText = $singleHouseHtmlDesc->plaintext;
$houseFile = file_get_contents("houses.txt");
if(strpos($houseFile, $houseUrl) == false) {
$linkData = [
'link' => $houseUrl,
'message' => $singleHouseHtmlDescText
];
$pageAccessToken ='xxxxxxxxx';
try {
$response = $fb->post('/me/feed', $linkData, $pageAccessToken);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: '.$e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: '.$e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();
}
$houses[] = [
'url' => $houseUrl,
'desc' => $singleHouseHtmlDesc
];
$i++;
}
var_dump($houses);
答案 0 :(得分:1)
strpos
函数无法检查所有行(当您使用\ n时)。所以用preg_match替换它(带s
标志),你就完成了:
if (!preg_match("~$houseUrl~s", $houseFile)) {
您的优化代码:
<?php
define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__ . '/src/Facebook/');
require_once __DIR__ . '/src/Facebook/autoload.php';
require 'simple_html_dom.php';
$fb = new Facebook\Facebook([
'app_id' => 'xxxxxxxx',
'app_secret' => 'xxxxxxxx',
'default_graph_version' => 'v2.8',
]);
$html = file_get_html('https://www.trulia.com/for_sale/McKinney,TX/300000p_price/SINGLE-FAMILY_HOME_type/date;d_sort/');
$houses = [];
$house = $html->find('.cardContainer', 0);
$houseLink = $house->find('a.tileLink', 0);
$houseUrl = 'https://trulia.com' . $houseLink->href;
file_put_contents("houses.txt", ($houseUrl . "\n"), FILE_APPEND);
$singleHouseHtml = file_get_html($houseUrl);
$singleHouseHtmlDesc = $singleHouseHtml->find('span#corepropertydescription', 0);
$singleHouseHtmlDescText = $singleHouseHtmlDesc->plaintext;
$houseFile = file_get_contents("houses.txt");
if (!preg_match("~$houseUrl~s", $houseFile)) {
$linkData = [
'link' => $houseUrl,
'message' => $singleHouseHtmlDescText,
];
$pageAccessToken = 'xxxxxxxxx';
try {
$response = $fb->post('/me/feed', $linkData, $pageAccessToken);
} catch (Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch (Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();
}
$houses[] = [
'url' => $houseUrl,
'desc' => $singleHouseHtmlDesc,
];
var_dump($houses);