J
J
Johnny2015-10-21 18:35:21
PHP
Johnny, 2015-10-21 18:35:21

How to execute cURL request in PHP?

Tell me how to execute this cURL request in PHP:

curl -u testclient:testpass http://api.pleer.com/token.php -d 'grant_type=client_credentials'

I am a beginner, and with curl, and in general, I work for the first time, I try like this:
$client_credentials = "testclient:testpass";

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://api.pleer.com/token.php");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $client_credentials);
$return = curl_exec($curl);
curl_close($curl);

print_r($return);

Documentation: player.com/api

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
Johnny, 2015-10-21
@GreysonKind

Google knows everything

Is this helpful for newbies?
Before asking someone a question, I will first dig through the floor of the Internet myself ..
Maybe it will come in handy for someone, I did this:
// set POST variables
$url 		= 'http://api.pleer.com/token.php';
$userpwd 	= 'testclient:testpass '; // заменить на свои данные
$fields 	= array(
  'grant_type' => 'client_credentials'
);

// url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string, '&');

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_USERPWD, $userpwd);

$result = curl_exec($ch);
curl_close($ch);

print_r('<pre>' . $result . '</pre>');

I
Ivanq, 2015-10-21
@Ivanq

As expected to answer such questions, Google knows everything

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question