user253530 user253530 - 3 years ago 96
PHP Question

php-curl script that saves images---actually captcha images

I have a curl class, called Curl.

Let's presume i have this code:

$url = ''
$fields = array('q'=>'search term'); //maybe some other arguments. but let's keep it simple.
$curl = new Curl();
$page = $curl->post($url,$fields);

$page will have some images wich curl doesn't load them by default. I need to know how i can save a specific image without using curl. Once I use $page = $curl->post(..) I need to know how I can have that image saved without using another $curl->post(_image_location_) to get that file.

The reason why need this is to save a captcha image from a form. I need to access the form and get that specific image that's being loaded. If i try to access the URL of the image, it will be a different captcha image.

Answer Source

What you are describing isn't possible. For every external resource inside a web page (ie anything that's not part of the HTML content itself, such as images, scripts, stylesheets, etc), you have to make a separate request to retrieve it. This is how all browsers operate.

Many captchas work on a session basis. You initial request to the HTML page is likely creating a session cookie which would be sent back as part of the response headers. This cookie will be expected when the image is requested. If you just do a plain curl request for the image, you won't be sending that cookie, and thus you'll get a different image.

You will have to analyze the page and determine exactly what kind of session management is going on, and modify your Curl request appropriately, but as I mentioned, I suspect it'll be cookie based. You'll probably want to look at the CURLOPT_COOKIEJAR curl_setopt() parameter to get things started. You can google for pretty straightforward examples as well.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download