InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Fetching a URL with an Arbitrary Method and POST Body in PHP
PHP

Fetching a URL with an Arbitrary Method and POST Body in PHP

InfinityCoder December 20, 2016

You want to request a URL using any method, such as POST, PUT, or DELETE. Your POST or PUT request may contain formatted data, such as JSON or XML.

Set the method, header, and content stream context options when using the http stream:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$url = 'http://www.example.com/meals/123';
$header = "Content-Type: application/json";
// The request body, in JSON
$body = '[{
    "type": "appetizer",
    "dish": "Chicken Soup"
}, {
    "type": "main course",
    "dish": "Fried Monkey Brains"
}]';
 
$options = array('method' => 'put',
    'header' => $header,
    'content' => $body);
// Create the stream context
$context = stream_context_create(array('http' => $options));
// Pass the context to file_get_contents()
print file_get_contents($url, false, $context);

With cURL, set the CURLOPT_CUSTOMREQUEST option to the method name. To include a request body, set CURLOPT_HTTPHEADER to the Content-Type and CURLOPT_POST FIELDS to the body:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$url = 'http://www.example.com/meals/123';
// The request body, in JSON
$body = '[{
    "type": "appetizer",
    "dish": "Chicken Soup"
}, {
    "type": "main course",
    "dish": "Fried Monkey Brains"
}]';
$c = curl_init($url);
curl_setopt($c, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($c, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($c, CURLOPT_POSTFIELDS, $body);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($c);
curl_close($c);

In HTTP_Request2, call setMethod() with a method constant, setHeader() with the Content-Type, and setBody() with the contents of the request body:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
require 'HTTP/Request2.php';
$url = 'http://www.example.com/meals/123';
// The request body, in JSON
$body = '[{
    "type": "appetizer",
    "dish": "Chicken Soup"
}, {
    "type": "main course",
    "dish": "Fried Monkey Brains"
}]';
$r = new HTTP_Request2($url);
$r->setMethod(HTTP_Request2::METHOD_PUT);
$r->setHeader('Content-Type', 'application/json');
$r->setBody($body);
 
$page = $r->send()->getBody();

In many REST-style APIs, you need to use more than just GET and POST to modify resources, you also need to use PUT and DELETE.
The examples in the Solution make HTTP PUT requests to set a dinner menu, with data formatted in JSON. If your data is in another format, such as XML, change the Content- Type accordingly.

If there is no body, such as in a HTTP DELETE request, only set the method.
The PUT method is often used for creating or modifying the contents of a specific resource.

cURL has three special options to help with this: CURLOPT_PUT, CURLOPT_IN FILE, and CURLOPT_INFILESIZE.

To upload a file with PUT and cURL, set CURLOPT_PUT to true, CURLOPT_INFILE to a filehandle opened to the file that should be uploaded, and CURLOPT_INFILESIZE to the size of that file. This is shown in Example 14-1.
Example 14-1. Uploading a file with cURL and PUT

1
2
3
4
5
6
7
8
9
10
11
$url = 'http://www.example.com/upload.php';
$filename = '/usr/local/data/pictures/piggy.jpg';
$fp = fopen($filename,'r');
$c = curl_init($url);
curl_setopt($c, CURLOPT_PUT, true);
curl_setopt($c, CURLOPT_INFILE, $fp);
curl_setopt($c, CURLOPT_INFILESIZE, filesize($filename));
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($c);
print $page;
curl_close($c);

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Sorting an Array by a Computable Field in PHP
You want to define your own sorting routine. Use usort() …

Sorting an Array by a Computable Field in PHP

Parsing Program Arguments with getopt in PHP
You want to parse program options that may be specified …

Parsing Program Arguments with getopt in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap