Php post to url example

PHP GET/POST request

PHP GET/POST request tutorial shows how to generate and process GET and POST requests in PHP. We use plain PHP and Symfony, Slim, and Laravel frameworks.

$ php -v php -v PHP 8.1.2 (cli) (built: Aug 8 2022 07:28:23) (NTS) .

HTTP

The is an application protocol for distributed, collaborative, hypermedia information systems. HTTP protocol is the foundation of data communication for the World Wide Web.

HTTP GET

The HTTP GET method requests a representation of the specified resource.

  • should only be used to request a resource
  • parameters are displayed in the URL
  • can be cached
  • remain in the browser history
  • can be bookmarked
  • should never be used when dealing with sensitive data
  • have length limits

HTTP POST

The HTTP POST method sends data to the server. It is often used when uploading a file or when submitting a completed web form.

  • should be used to create a resource
  • parameters are not displayed in the URL
  • are never cached
  • do not remain in the browser history
  • cannot be bookmarked
  • can be used when dealing with sensitive data
  • have no length limits
Читайте также:  Php mysql utf8 знаки вопроса

PHP $_GET and $_POST

PHP provides the $_GET and $_POST superglobals. The $_GET is an associative array of variables passed to the current script via the URL parameters (query string). The $_POST is an associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.

PHP GET request

In the following example, we generate a GET request with curl tool and process the request in plain PHP.

 $message = $_GET['message']; if ($message == null) < $message = 'hello there'; >echo "$name says: $message";

The example retrieves the name and message parameters from the $_GET variable.

$ php -S localhost:8000 get_req.php
$ curl 'localhost:8000/?name=Lucia&message=Cau' Lucia says: Cau $ curl 'localhost:8000/?name=Lucia' Lucia says: hello there

We send two GET requests with curl.

PHP POST request

In the following example, we generate a POST request with curl tool and process the request in plain PHP.

 $message = $_POST['message']; if ($message == null) < $message = 'hello there'; >echo "$name says: $message";

The example retrieves the name and message parameters from the $_POST variable.

$ php -S localhost:8000 post_req.php
$ curl -d "name=Lucia&message=Cau" localhost:8000 Lucia says: Cau

We send a POST request with curl.

PHP send GET request with Symfony HttpClient

Symfony provides the HttpClient component which enables us to create HTTP requests in PHP.

$ composer req symfony/http-client

We install the symfony/http-client component.

request('GET', 'http://localhost:8000', [ 'query' => [ 'name' => 'Lucia', 'message' => 'Cau', ] ]); $content = $response->getContent(); echo $content . "\n";

The example sends a GET request with two query parameters to localhost:8000/get_request.php .

$ php -S localhost:8000 get_req.php
$ php send_get_req.php Lucia says: Cau

We run the send_get_req.php script.

PHP send POST request with Symfony HttpClient

In the following example, we send a POST request with Symfony HttpClient.

request('POST', 'http://localhost:8000', [ 'body' => [ 'name' => 'Lucia', 'message' => 'Cau', ] ]); $content = $response->getContent(); echo $content . "\n";

The example sends a POST request with two parameters to localhost:8000/post_req.php .

$ php -S localhost:8000 post_req.php
$ php send_post_req.php Lucia says: Cau

We run the send_post_req.php script.

PHP GET request in Symfony

In the following example, we process a GET request in a Symfony application.

$ symfony new symreq $ cd symreq

A new application is created.

$ composer req annot $ composer req maker --dev

We install the annot and maker components.

$ php bin/console make:controller HomeController

We create a new controller.

) */ public function index(Request $request): Response < $name = $request->query->get('name', 'guest'); $message = $request->query->get('message', 'hello there'); $output = "$name says: $message"; return new Response($output, Response::HTTP_OK, ['content-type' => 'text/plain']); > >

Inside the HomeController’s index method, we get the query parameters and create a response.

$name = $request->query->get('name', 'guest');

The GET parameter is retrieved with $request->query->get . The second parameter of the method is a default value which is used when no value was retrieved.

$ curl 'localhost:8000/?name=Lucia&message=Cau' Lucia says: Cau

We generate a GET request with curl.

PHP POST request in Symfony

In the following example, we process a POST request in a Symfony application.

) */ public function index(Request $request): Response < $name = $request->request->get('name', 'guest'); $message = $request->request->get('message', 'hello there'); $output = "$name says: $message"; return new Response($output, Response::HTTP_OK, ['content-type' => 'text/plain']); > >

We change the controller to process the POST request.

$name = $request->request->get('name', 'guest');

The POST parameter is retrieved with $request->request->get . The second parameter of the method is a default value which is used when no value was retrieved.

$ curl -d "name=Lucia" localhost:8000 Lucia says: hello there

We generate a POST request with curl.

PHP GET request in Slim

In the following example, we are going to process a GET request in the Slim framework.

$ composer req slim/slim $ composer req slim/psr7 $ composer req slim/http

We install slim/slim , slim/psr7 , and slim/http packages.

get('/', function (Request $request, Response $response): Response < $name = $request->getQueryParam('name', 'guest'); $message = $request->getQueryParam('message', 'hello there'); $output = "$name says $message"; $response->getBody()->write($output); return $response; >); $app->run();

We get the parameters and return a response in Slim.

$name = $request->getQueryParam('name', 'guest');

The query parameter is retrieved with getQueryParam ; the second parameter is the default value.

$response->getBody()->write($output);

We write the output to the response body with write .

$ php -S localhost:8000 -t public
$ curl 'localhost:8000/?name=Lucia&message=Cau' Lucia says: Cau

We generate a GET request with curl.

PHP POST request in Slim

In the following example, we are going to process a POST request in the Slim framework.

post('/', function (Request $request, Response $response): Response < $data = $request->getParsedBody(); $name = $data['name']; $message = $data['message']; if ($name == null) < $name = 'guest'; >if ($message == null) < $message = 'hello there'; >$output = "$name says: $message"; $response->getBody()->write($output); return $response; >); $app->run();

We get the POST parameters and return a response in Slim.

$data = $request->getParsedBody();

The POST parameters are retrieved with getParsedBody .

$ php -S localhost:8000 -t public
$ curl -d "name=Lucia" localhost:8000 Lucia says: hello there

We generate a POST request with curl.

PHP GET request in Laravel

In the following example, we process a GET request in Laravel.

$ laravel new larareq $ cd larareq

We create a new Laravel application.

query('name', 'guest'); $message = $request->query('message', 'hello there'); $output = "$name says $message"; return $output; >);

We get the GET parameters and create a response.

$ curl 'localhost:8000/?name=Lucia&message=Cau' Lucia says Cau

We send a GET request with curl.

PHP POST request in Laravel

In the following example, we send a POST request from an HTML form.

We have a POST form in a Blade template. Laravel requires CSRF protection for POST requests. We enable CSRF protection with @csrf .

); Route::post('/process_form', function (Request $request) < $request->validate([ 'name' => 'required|min:2', 'message' => 'required|min:3' ]); $name = $request->input('name'); $message = $request->input('message'); $output = "$name says: $message"; return $output; >);

We validate and retrieve the POST parameters and send them in the response. This example should be tested in a browser.

In this tutorial, we have worked with GET and POST requests in plain PHP, Symfony, Slim, and Laravel.

Источник

Php send post data to url code example

For example, add the following after the array definition: then remove the following lines: and Complete code (tested): test.php testaction.php output: Solution 2: Part of is already json encoded. You want to add something like: Solution 3: Try this instead Solution 1: you can use cURL library for posting data: http://www.php.net/curl where postfield contains XML you need to send — you will need to name the postfield the API service (Clickatell I guess) expects Solution 2: Another option would be :

Sending data to a webservice using post

You’re passing the POST data in JSON format, try to pass it in the form k1=v1&k2=v2. For example, add the following after the $data array definition:

then remove the following lines:

curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json")); 
 'fooToken', 'json' => '', ); foreach($data as $key=>$value) < $content .= $key.'='.$value.'&'; >$curl = curl_init($url); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $content); $json_response = curl_exec($curl); $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); $response = json_decode($json_response, true); var_dump($response); ?> 

testaction.php

array(2) < 'token' =>string(8) "fooToken" 'json' => string(14) "" > 

Part of $data is already json encoded. Try making $data pure php. ie $data[‘json’]=array(‘foo’=>’test’);

PHP, cURL, and HTTP POST example?, Can anyone show me how to do a PHP cURL with an HTTP POST? I want to send data like this: username=user1, password=passuser1, gender=1 To www.example.com I expect the cURL to return a response like . Stack Overflow. About; Products For Teams; Stack Overflow Public questions & answers; …

PHP Post data with Fsockopen

There are many small errors in your code. Here’s a snippet which is tested and works.

 'world' ); $content = http_build_query($vars); fwrite($fp, "POST /reposter.php HTTP/1.1\r\n"); fwrite($fp, "Host: example.com\r\n"); fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n"); fwrite($fp, "Content-Length: ".strlen($content)."\r\n"); fwrite($fp, "Connection: close\r\n"); fwrite($fp, "\r\n"); fwrite($fp, $content); header('Content-type: text/plain'); while (!feof($fp))

And then at example.com/reposter.php put this

When run you should get output something like

HTTP/1.1 200 OK Date: Wed, 05 Jan 2011 21:24:07 GMT Server: Apache X-Powered-By: PHP/5.2.9 Vary: Host Content-Type: text/html Connection: close 1f Array ( [hello] => world ) 0 

At no point is $data being written to the socket. You want to add something like:

$out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); fwrite($fp, $data); 
$out .= 'Content-Length: ' . strlen($data) . '\r\n'; $out .= "Connection: Close\r\n\r\n"; $out .= $data; 

Sending POST data using cURL, by scanning QR code, Then the ‘QR code scanner app’, needs to open a browser, which shows check.php with the form AND the passed data as the values of the input fields. Now, I get only the response of check.php (plain text).

Sending XML data using HTTP POST with PHP

you can use cURL library for posting data: http://www.php.net/curl

$ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_URL, "http://websiteURL"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, "XML=".$xmlcontent."&password=".$password."&etc=etc"); $content=curl_exec($ch); 

where postfield contains XML you need to send — you will need to name the postfield the API service (Clickatell I guess) expects

Another option would be file_get_contents() :

// $xml_str = your xml // $url = target url $post_data = array('xml' => $xml_str); $stream_options = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded' . "\r\n", 'content' => http_build_query($post_data))); $context = stream_context_create($stream_options); $response = file_get_contents($url, null, $context); 

How to make a POST request with PHP given the, I’ve been trying to make a POST request with PHP with something called the WorkWave API. This is the code they provide for making a POST request to set the app’s callback URL: POST /api/v1/callback HTTP/1.0 Accept: application/json X-WorkWave-Key: YOUR API KEY Host: wwrm.workwave.com …

Источник

Оцените статью