PHP

BASIC PHP CURL EXAMPLES

1) CURL FETCH

INIT + OPTIONS $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://site.com"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   FETCH! echo curl_exec($ch); curl_close($ch);

2) CURL SAVE

INIT + OPEN FILE $ch = curl_init(); $fh = fopen("saved.html", "w"); curl_setopt($ch, CURLOPT_URL, "http://site.com"); curl_setopt($ch, CURLOPT_FILE, $fh);   DOWNLOAD! curl_exec($ch); curl_close($ch); fclose($fh);

3) CURL POST

INIT + OPTIONS $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://site.com"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, ["KEY" => "VALUE"]);   FETCH! echo curl_exec($ch); curl_close($ch);

4) CURL GET

URL QUERY STRING $url = "http://site.com/page/?"; $params = ["KEY" => "VALUE"]; foreach ($params as $k=>$v) { $url .= urlencode($k)."=".urlencode($v)."&"; } $url = substr($url, 0, -1);   INIT + OPTIONS $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   FETCH! echo curl_exec($ch); curl_close($ch);