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);

 

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published.