Example of using CURL to get a page that outputs json

	$Url = "mydomain.org/somefile.php?MyParameter1=abc&MyParameter2=2";
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $Url);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 		//Follow any page redirects etc (optional)
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);				//<<<Include this for the output of curl to go to the curl_exec() output below instead of the browser screen
	curl_setopt($ch, CURLOPT_HTTPGET, true);					//You don't have to include this for it to work
	$CurlResult= curl_exec($ch);
	curl_close($ch);

	//If your output is json then you can decode it with this:
	$DecodedJson = json_decode($CurlResult);
	echo "CurlResult: ";
	print_r($CurlResult);
	echo "<br>DecodedJson: ";
	print_r($DecodedJson);
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *