Php post request api

How to Send a Post Request with PHP

In this tutorial, we aim share with you 3 competent ways of sending post requests using PHP.

Follow the examples below and choose the one that meets your needs better.

A. Using PHP Curl

The PHP cURL extension is a quite straightforward way allowing to combine different flags with setopt() calls.

Below is an example with an $xml variable that holds the XML prepared for sending:

 $url = 'http://api.flickr.com/services/xmlrpc/'; $ch = curl_init($url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); ?>

As you can notice from the example above, first, the connection is initialised, and then some options are set with setopt() . These actions inform PHP about making a post request.

B. Using Pecl_Http

Generally, Pecl_Http combines two interfaces. The first one is considered procedural, the second one- object-oriented . Let’s start at discovering the first one. It provides you with a simpler way than the curl. Here is a script, which is translated for pecl_http :

 $url = 'http://api.flickr.com/services/xmlrpc/'; $response = http_post_data($url, $xml); ?>

C. Using the Object-Oriented (OO) Interface of Pecl_HTTP

As it was noted above, the second interface of xpecl_http is considered object- oriented . It is similar to the 2 extensions demonstrated above but applies a different interface. Here is how its code looks like:

 $url = 'http://api.flickr.com/services/xmlrpc/'; $request = new HTTPRequest($url, HTTP_METH_POST); $request->setRawPostData($xml); $request->send(); $response = $request->getResponseBody(); ?>

You can notice that this code is longer than the previous one. You might think that it is more complicated, yet it is as powerful and flexible as the previous ones. So, it can be an outstanding option to implement in your practice.

Источник

Как сделать API запрос на PHP?

Дали пример того как должен выглядеть php запрос и все никак не дойдет что с ним надо делать, как оборачивать и как отправлять, можете подсказать?

Пример: HTTPie-cli: http --form POST https://api.saures.ru/login email=demo@saures.ru password=demo -v
HTTP-запрос POST /login HTTP/1.1 Accept: */* Accept-Encoding: gzip, deflate Connection: keep-alive Content-Length: 36 Content-Type: application/x-www-form-urlencoded; charset=utf-8 Host: api.saures.ru User-Agent: HTTPie/0.9.8 email=demo%40saures.ru&password=demo
HTTP-ответ HTTP/1.1 200 OK Connection: keep-alive Content-Length: 98 Content-Type: application/json; charset=utf-8 < "data": < "role": 1, "sid": "b731f212-f5ee-43de-a076-55f6de9fd690" >, "errors": [], "status": "ok" >

все что смог дак это собрать вот такую штуку, она естественно не работает, ошибку говорит

class ApiController extends Controller < public function api1()< $url='https://api.saures.ru/login'; // массив для переменных, которые будут переданы с запросом $opts = array( 'http' =>array( 'method' => 'POST', // метод передачи данных 'header' => 'Content-type: application/x-www-form-urlencoded;charset=utf-8', // заголовок 'email' => 'demo@40saures.ru', 'password' => 'demo' ) ); $context = stream_context_create($opts); // создаём контекст потока $result = file_get_contents($url, false, $context); //отправляем запрос dd($result) ; > >

Простой 1 комментарий

Источник

Читайте также:  Html связать страницы между собой
Оцените статью