2

I want to send data to an API. The data include simple variables: username, password, email, etc. The problem is that O want to send data to this using POST method. I searched this issue on Google, and everyone is saying to go for CURL.

What is CURL? Is it a function, script, API or what?

Is there any other way to do it?

I want to send something like this:

$username, $password to www.abc.com?username=$username&password=$password

Best Regards

RyanS
  • 627
  • 1
  • 10
  • 26
theGame
  • 383
  • 4
  • 6
  • 18

5 Answers5

4

Simply use: file_get_contents()

// building array of variables
$content = http_build_query(array(
            'username' => 'value',
            'password' => 'value'
            ));
// creating the context change POST to GET if that is relevant 
$context = stream_context_create(array(
            'http' => array(
                'method' => 'POST',
                'content' => $content, )));

$result = file_get_contents('http://www.example.com', null, $context);
//dumping the reuslt
var_dump($result);
wpcoder
  • 1,016
  • 11
  • 17
4

By using this function you can send a post request with an optional number of parameters. Just fill in the postdata array with key values pairs. Example usage are provided.

<?php

function do_post_request($url, $postdata)
{
  $content = "";

  // Add post data to request.
  foreach($postdata as $key => $value)
  {
    $content .= "{$key}={$value}&";
  }

  $params = array('http' => array(
    'method' => 'POST',
    'header' => 'Content-Type: application/x-www-form-urlencoded',
    'content' => $content
  ));

  $ctx = stream_context_create($params);
  $fp = fopen($url, 'rb', false, $ctx);

  if (!$fp) {
    throw new Exception("Connection problem, {$php_errormsg}");
  }

  $response = @stream_get_contents($fp);
  if ($response === false) {
    throw new Exception("Response error, {$php_errormsg}");
  }

  return $response;
}

// Post variables.
$postdata = array(
  'username' => 'value1',
  'password' => 'value2',
  'param3' => 'value3'
);

do_post_request("http://www.example.com", $postdata);
?>
SumoSudo
  • 3
  • 4
JK.
  • 5,126
  • 1
  • 27
  • 26
2

cURL is a library "that allows you to connect and communicate to many different types of servers with many different types of protocols". So you can use cURL to send an HTTP POST request. Please read the PHP manual for detailed information: http://www.php.net/manual/en/book.curl.php

But cURL is not the only answer. You can use PHP Streams, or even connect to the webserver using socket functions and then generate your requests.

Personally I often use cURL because it's very flexible and you can work with cookies easily.

lbrandao
  • 4,144
  • 4
  • 35
  • 43
  • 1
    Why not include an example of using cURL to actually send Post data with your answer? – n8bar Apr 21 '17 at 17:50
1

Php curl manual is a good starting point.

Following is an example of the usage of curl in php on Stackoverflow:

Send json post using php

Community
  • 1
  • 1
Ozair Kafray
  • 13,351
  • 8
  • 59
  • 84
1

The data set in an xml file:

$post="<?xml version="1.0\" encoding=\"UTF-8\"?>
<message xmlns=\"url\"   >
<username>".$username."</username>
<password>".$password."</password>

<status date=\"2010-11-05 14:44:10+02\"/>
<body content-type=\"text/plain\">Noobligatory text</body>
</message>";

$url = "a-url-to-send-the-data";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 6);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
curl_close($ch);
Vadim
  • 31
  • 6