how to pass JSON via curl with php

function jsoncurl($url, $fieldString){
	//open connection
	$options = array(
		CURLOPT_RETURNTRANSFER => true, // return web page
		CURLOPT_FOLLOWLOCATION => true, // follow redirects
		CURLOPT_USERAGENT => "spider", // who am i
		CURLOPT_AUTOREFERER => true, // set referer on redirect
		CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
		CURLOPT_TIMEOUT => 120, // timeout on response
		CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
	);
	$ch = curl_init();

	//set the url, number of POST vars, POST data
	curl_setopt($ch,CURLOPT_URL,$url);

	$json=json_encode($fieldString);
	//for some diff
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
	curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $json );
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt_array( $ch, $options );
	//execute post
	$result = curl_exec($ch); 
	$header = curl_getinfo( $ch );

	//close connection
	curl_close($ch);
	return $result;
}

$result=jsoncurl($url, $fieldString);

Ref :

http://stackoverflow.com/questions/7279576/how-to-create-the-rest-api-for-this-curl-operation-in-php/7279774#7279774

Advertisements

About rahul23134654

Hi, I am Rahul Meha , B.E. in (I.T.)
This entry was posted in PHP. Bookmark the permalink.

One Response to how to pass JSON via curl with php

  1. Pingback: Lazuli » [PHP] POSTメソッドでJSONを送る処理について

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s