Skip to content

Instantly share code, notes, and snippets.

@divinity76
Created April 29, 2020 20:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save divinity76/c2eb93388e693f6baeb17f754f7a57e5 to your computer and use it in GitHub Desktop.
Save divinity76/c2eb93388e693f6baeb17f754f7a57e5 to your computer and use it in GitHub Desktop.
duckduckgo image search libcurl php
<?php
declare(strict_types=1);
$search_query="Nintendo 64 controller";
require_once('hhb_.inc.php');
$hc=new hhb_curl('',true);
$hc->setopt_array(array(
CURLOPT_CONNECTTIMEOUT=>10,
CURLOPT_TIMEOUT=>20,
// without a fake useragent, you will get the error: <div id="error_homepage" style="display:block">Oops, there was an error. &nbsp;Please try again.<br><br>If it persists, please email error@duckduckgo.com<br><img src="https://improving.duckduckgo.com/t/fpbot">
CURLOPT_USERAGENT=>'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0',
));
// this is required to get a "vqd" token,
// some shit duckduckgo use instead of cookies..?
$url='https://duckduckgo.com/?'.http_build_query(array(
'q'=>$search_query,
't'=>'h_',
'iax'=>'images',
'ia'=>'images'
));
$html=$hc->exec($url)->getStdOut();
// vqd='3-142216309694420347760037715005911496568-220342727435306949816443430106535095457';
$vqd_token='';
if(!preg_match("/vqd\s*\=\s*\'(?<vqd_token>[^\']*)/",$html,$matches)){
var_dump($html);
die("error: unable to extract vqd token");
}
$vqd_token=$matches['vqd_token'];
$url2='https://duckduckgo.com/d.js?'.http_build_query(array (
'q' => $search_query,
't' => 'A',
'l' => 'us-en',
's' => '0',
'a' => 'h_',
'dl' => 'en',
'ct' => 'NO',
'ss_mkt' => 'us',
'vqd' => $vqd_token,
'p_ent' => 'information appliance',
'ex' => '-1',
'sp' => '0',
'v7exp' => 'a',
'sltexp' => 'a',
)
);
$html=$hc->exec($url2)->getStdOut();
// "results":[{"title":"Nintendo 64 controller
if(!preg_match("/\\\"results\\\"\\:(?<images_json>.+?\\}\\])/",$html,$matches)){
die("error: unable to extract images json...");
}
$images_json=$matches["images_json"];
$images=json_decode($images_json,true);
var_dump($images);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment