PHPSOFT SOLUTIONS LOGO
  • Home
  • About Us
  • Contact Us
  • Contact Us
  • Blog
  • Online Courses
  • Contact US

how to use Curl in php

cURL is a command-line application for performing requests using a variety of protocols including HTTP. cURL is often used by developers to test Google Data services(cURL is interact with google services), as it supports the HTTP functionality required to interact with the APIs.

how to set header Curl in php:-curl_setopt($ch,CURLOPT_HTTPHEADER,array('HeaderName: HeaderValue'));

code for post:

<?php

$data = array(‘custumer_name’ => ‘John’,
’email’ => ‘john@example.com’,
‘mobile_number’ => ‘1234567890’,
);
curlUsingPost(‘http://localhost/yii/blog/index.php/api/requests’, $data);
$data;
$url = ‘http://localhost/yii/blog/index.php/api/requests’;

?>

<?php

function curlUsingPost($url, $data)
{

if(empty($url) OR empty($data))
{
return ‘Error: invalid Url or Data’;
}
//url-ify the data for the POST
$fields_string = ”;
foreach($data as $key=>$value) { $fields_string .= $key.’=’.$value.’&’; }
$fields_string = rtrim($fields_string,’&’);

//die(‘error’);
//open connection
$ch = curl_init();

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

curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10); # timeout after 10 seconds, you can increase it
curl_setopt($ch,CURLOPT_HTTPHEADER,array(‘X_REQUES_USERNAME: demo’,’X_REQUES_PASSWORD: demo’));
// curl_setopt($ch,CURLOPT_HEADER,true);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); # Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_USERAGENT , “Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)”); # Some server may refuse your request if you dont pass user agent

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

//execute post
$result = curl_exec($ch);var_dump($result);

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

 

Posted on February 2014
Category: PHP Frameworks

Categories

  • Blog
  • phonegap
  • PHP Frameworks
  • php-jquery
  • system
  • Wordpress
  • Yii

Post navigation

Previous post

phonegap adding plugin to local app

Next post

Phonegap access origin error resolved


PHPSOFT SOLUTIONS LOGO

Our mission is to satisfy our customers to the best.Money is the source nowdays, the trust of how we can use your money in the best way is build by us.The core value of php soft solutions is to provide the best service to our customers in a great manner.

Contact

PLOT NO. C157, The Groove, Industrial focal point, Phase7,MOHALI, SAS Nagar, Punjab, 160055

8146501814

contact@phpsoftsolutions.in

Pages Links
  • WordPress
  • Yii
  • PHP Frameworks
  • php-jquery
  • system
  • phonegap

© 2025 PHPSOFT SOLUTIONS