public static function parseNewhomesourceProperty($url) { if (!$url) { return null; } $dataSourceId = 1001; $html = new \simple_html_dom(); $httpClient = new GuzzleHttp\Client(); try { $contents = $httpClient->get($url)->getBody()->getContents(); } catch (GuzzleHttp\Exception\RequestException $e) { throw new FunFangException("访问 {$url} 时出错!" . $e->getMessage()); } $html->load($contents); $planIdEle = $html->find('#PlanId', 0); if (!$planIdEle) { throw new FunFangException('您输入的URL可能不正确,解析失败!'); } $planId = $planIdEle->value; //先从数据库中查找 if ($planId) { $record = Property::where('DataSourceId', $dataSourceId)->where('DataId', $planId)->first(); if ($record) { $html->clear(); return $record; } } //如果数据库中没有记录,从html中解析并保存到数据库 $record = new Property(); $record->DataSourceId = $dataSourceId; $record->DataId = trim($planId); $record->ReferenceUrl = $url; $communityId = $html->find('#CommunityId', 0)->value; $specId = $html->find('#SpecId', 0)->value; //照片需要通过另外的API获取,必须的参数:communityId、planId、specId、isPreview $photoRes = $httpClient->post('http://www.newhomesource.com/detailgetgallery', ['form_params' => ['communityId' => $communityId, 'planId' => $planId, 'specId' => $specId, 'isPreview' => 'False']]); $photoObj = json_decode($photoRes->getBody()->getContents()); $photoUrls = []; foreach ($photoObj->PropertyMediaLinks as $photo) { //type:i是图片,v是视频 if ($photo->Type == 'i') { array_push($photoUrls, 'http://nhs-dynamic.bdxcdn.com/' . $photo->Url); } } $record->PhotoUrls = implode(',', $photoUrls); //价格 $listPrice = $html->find('#nhs_DetailsDescriptionAreaWrapper .nhs_DetailsPrice span', 0)->plaintext; $record->ListPrice = str_replace(',', '', str_replace('$', '', $listPrice)); foreach ($html->find('#nhs_HomeDetailsHeaderBrandHomesSqFt ul li') as $li) { $text = $li->plaintext; if (StringUtil::containsIgnoreCase($text, 'Bedrooms')) { //卧室数 $bedrooms = str_ireplace('Bedrooms', '', $text); $bedrooms = str_replace(' ', '', $bedrooms); $record->Bedrooms = $bedrooms; } else { if (StringUtil::containsIgnoreCase($text, 'Bathrooms')) { //浴室数 $bathrooms = str_ireplace('Bathrooms', '', $text); $bathrooms = str_replace(' ', '', $bathrooms); $record->BathsFull = intval($bathrooms); $record->BathsHalf = intval($bathrooms) == $bathrooms ? 0 : 1; } else { if (StringUtil::containsIgnoreCase($text, 'sq.ft.')) { //面积 $structureSqFt = str_ireplace('sq.ft.', '', $text); $structureSqFt = str_replace(',', '', $structureSqFt); $record->StructureSqFt = $structureSqFt; } else { if (StringUtil::containsIgnoreCase($text, 'Garages')) { //停车位 $garageSpaces = str_ireplace('Garages', '', $text); $garageSpaces = str_replace(' ', '', $garageSpaces); $record->GarageSpaces = $garageSpaces; } } } } } $description = $html->find('#nhs_DetailDescriptionArea', 0)->plaintext; $description = str_replace(' ', '', $description); $record->Description = $description; //解析坐标位置 $jsonStr = $html->find('#nhs_HomeDetailv2 script', 0)->innertext; $obj = json_decode($jsonStr); $lat = $obj->Geo->latitude; $lng = $obj->Geo->longitude; $addressRes = $httpClient->get("http://ditu.google.cn//maps/api/geocode/json?latlng={$lat},{$lng}"); $addressJson = json_decode($addressRes->getBody()->getContents()); if ($addressJson->status == 'OK') { $addressObj = GoogleGeoHelper::getAddress($addressJson->results); } if ($addressObj) { $record->State = $addressObj->state; $record->County = $addressObj->county; $record->City = $addressObj->city; $streetNumber = property_exists($addressObj, 'streetNumber') ? $addressObj->streetNumber : ''; $street = property_exists($addressObj, 'street') ? $addressObj->street : ''; $record->Address = "{$streetNumber} {$street}"; $record->PostalCode = $addressObj->postalCode; //$record->Location = DB::raw("GeomFromText('POINT($addressObj->lng $addressObj->lat)')"); $record->Location = "{$addressObj->lng},{$addressObj->lat}"; } $record->save(); $html->clear(); return $record; }
/** * 通过MLSID或第三方网站URL获取房源 * @param Request $request * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string * @throws \Exception */ public function fetch(Request $request) { $this->validate($request, ['type' => 'required', 'q' => 'required']); //搜索内容 $q = trim($request->q); //判断搜索类型 if ($request->type == 0) { //MLSID $model = Property::where('MLSNumber', $q)->first(); if ($model) { //return view('property._detail', ['record' => $record]); $view = view('property._detail', ['record' => $model]); return response()->json(['model' => $model, 'view' => $view]); } throw new Exception('未找到!'); } else { if ($request->type == 1) { //第三方网站URL $domain = UrlUtil::getMainDomain($q); if (StringUtil::equalsIgnoreCase($domain, 'loopnet.com')) { $model = Util::parseLoopnetProperty($q); $view = view('property._detail', ['model' => $model])->render(); return response()->json(['model' => $model, 'view' => $view]); } else { if (StringUtil::equalsIgnoreCase($domain, 'newhomesource.com')) { $model = Util::parseNewhomesourceProperty($q); $view = view('property._detail', ['model' => $model])->render(); return response()->json(['model' => $model, 'view' => $view]); } else { throw new Exception('您输入的网站URL暂不支持!'); } } } else { throw new Exception('搜索类型错误!'); } } }