-1
對於OOP,我很新,我試圖調用一次構造函數。PHP調用構造函數一次
我有一個具有Guzzle客戶端的構造函數的類,它也在網站上登錄。現在我有兩個其他類:訂單和順序是用構造函數擴展類(我不知道如果我做正確),我這樣做,因爲我認爲這將調用我的構造函數一次,但它仍然創建倍數客戶端
我的代碼如下所示:
RequestController.php:(有這個建築工使得狂飲客戶端)
namespace App\Http\Controllers\Scraper;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use GuzzleHttp\Client;
use Symfony\Component\DomCrawler\Crawler;
class RequestController extends Controller
{
public static $_instance = null;
public $client;
public function __construct(){
$headers = [
'user-Agent' => '',
'Host' => 'orders.example.com',
'Origin' => 'https://orders.example.com',
'Referer' => 'https://orders.example.com',
];
$form_params = [
'form_params' => [
'username' => env('USERNAME'),
'password' => env('PASSWORD'),
'login' => 'true',
'language' => 'en'
]
];
$this->client = new Client([
'headers' => $headers,
'cookies' => new \GuzzleHttp\Cookie\CookieJar
]);
echo 'new client has created <br>';
$this->client->post('https://orders.example.com/', $form_params);
return $this->client;
}
}
OrdersController.php:
<?php
namespace App\Http\Controllers\Scraper;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Scraper\RequestController;
use App\Http\Controllers\Scraper\OrderController;
use Symfony\Component\DomCrawler\Crawler;
// use GuzzleHttp\Client;
class OrdersController extends RequestController
{
public $order_ids = [];
public function orders(){
$response = $this->client->post('https://orders.example.com/orders/orders');
$crawler = new Crawler((String) $response->getBody());
$order_ids = $crawler->filterXPath('//tbody[contains(@class, "wide")]')->extract(['_text', 'rel']);
foreach($order_ids as $order_id){
$order = new OrderController();
$order->order(substr($order_id[1], 2));
}
}
}
OrderController.php:
<?php
namespace App\Http\Controllers\Scraper;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Scraper\RequestController;
class OrderController extends RequestController
{
private $_delivery_time,
$_order_price,
$_delivery_price,
$_total_price,
$_pay_option;
public function order($order_id){
$details = [
'form_params' => [
'id' => $order_id
]
];
$response = $this->client->post('https://orders.example.com/orders/details', $details);
// echo (string) $response->getBody();
// die();
}
}
如果任何人都可以給我一些意見,我將不勝感激,謝謝!!