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($cURL, CURLOPT_HTTPGET, true);					//You don't have to include this for it to work
	$result = curl_exec($ch);
	curl_close($ch);

	//If your output is json then you can decode it with this:
	$decoded_json = json_decode($result);
	echo "result:";
	print_r($result);
	echo "decoded_json:";
	print_r($decoded_json);

 

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.