Tuesday 15 April 2014

php - how can i use curl instead of fopen -



php - how can i use curl instead of fopen -

how can code below curl in php ?

because fopen problematic in server side. sample code snippet wiziq api usage...

class httprequest { function wiziq_do_post_request($url, $data, $optional_headers = null) { $params = array('http' => array( 'method' => 'post', 'content' => $data )); if ($optional_headers !== null) { $params['http']['header'] = $optional_headers; } $ctx = stream_context_create($params); $fp = fopen($url, 'r+',false, $ctx); if (!$fp) { throw new exception("problem $url, $php_errormsg"); } $response = @stream_get_contents($fp); if ($response === false) { throw new exception("problem reading info $url, $php_errormsg"); } homecoming $response; } }//end class httprequest

simple http curl request:

function get_response( $url, $post_params ) { $channel = curl_init(); curl_setopt($channel, curlopt_url, $url); curl_setopt($channel, curlopt_returntransfer, true); curl_setopt($channel, curlopt_connecttimeout, 5); curl_setopt($channel, curlopt_timeout, 5); curl_setopt($channel, curlopt_encoding, "gzip"); curl_setopt($channel, curlopt_verbose, true); curl_setopt($channel, curlopt_post, true); //set post request true curl_setopt($channel, curlopt_postfields, $post_params); //set post params curl_setopt($channel, curlopt_useragent, 'mozilla/5.0 (windows; u; windows nt 6.1; rv:2.2) gecko/20110201'); curl_setopt($channel, curlopt_followlocation, true); curl_setopt($channel, curlopt_ipresolve, curl_ipresolve_v4); $output = curl_exec($channel); curl_close( $channel ); homecoming $output; } $url = "http://www.stackoverflow.com"; $post_params = "your_fields"; $response = get_response($url, $post_params); echo $response;

php curl fopen

No comments:

Post a Comment