Emulate Browser Request by phpQuery - URL Rewrite modified the path

216 Views Asked by At

I am using phpQuery to simulate a browser request. Codes are simple as follow:

require_once('phpQuery/phpQuery.php');
phpQuery::browserGet($url, 'success1');
function success1($browser) {
    print $browser;
}

where URL is http://www.etk.cc/bmw/EN/parts/info/13547556118.

However, I received an Exception of:

Fatal error: Uncaught exception 'Zend_Uri_Exception' with message 'Path "/bmw/EN/parts/info/http://www.etk.cc/bmw/EN/parts/info/Throttle housing Assy/13547556118/" is not a valid HTTP path'

It is believed that URL Rewrite changed the URL to http://www.etk.cc/bmw/EN/parts/info/Throttle housing Assy/13547556118/ but the phpQuery or Zend library does not recognize the URL Rewrite.

How do I resolve this issue (loading the correct URL)?

Note: I can't use the URL after URL Rewrite, as the URL contains the product name & has no pattern.

1

There are 1 best solutions below

6
On

Here is my solution.

I first use the following codes to obtain the header of the request:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_NOBODY, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); 
$out = curl_exec($ch); 

From the Location: header, I can know the rewritten URL. Then, I use the phpQuery again to obtain the required information using the newly obtained rewritten URL.

Footnote: I do think the Exception in the question is a bug in phpQuery.