Curlopt_post

This is a short tutorial on how to upload a file using PHP’s cURL extension. The goal of this is to obviously use PHP to mimic an upload form. Php curloptpost; curl option list php; php curl conditionally add post field; php curl CURLOPTHTTPHEADER; curl setopt php update; php curl set header; php curlsetopt with headers; php curl get curloptpostfields; how to set http method in curl in php; curl options request php.

last modified February 8, 2021

PHP cURL tutorial shows how to work with cURL library in PHP. cURL is a wrapperover the libcurl library.

cURL

The curl is a command line tool and library for transferring data withURL. It supports multiple protocols including HTTP, HTTPS, FTP, GOPHER, MQTT,or SMTP. The cURL is a PHP wrapper over the library.

The cURL must be installed. For instance, on Debian the package name isphp-curl.

PHP cURL GET request

In the following examples, we create simple GET requests.

In the example, we send a GET request to a small website. The output is directlyshown in the standard output.

The curl_init function initializes a new session and returns a cURLhandle for use with the curl_setopt, curl_exec, and curl_close functions. We provice a URL to which we sent the request.

The curl_exec executes the given cURL session.

The curl_close closes the cURL session.

In the next example, we send the output of the transfer to a variable.

get_req2.php

With the curl_setopt we set options for the cURL transfer. TheCURLOPT_RETURNTRANSFER returns the transfer as a string of thereturn value of curl_exec instead of outputting it directly.

PHP cURL download file

The CURLOPT_FILE option specifies where the transfer should be written to; the default is the standard output.

In the example, we set CURLOPT_FILE option to a file handle, that we have created. With the CURLOPT_HEADER, we disable the header.

PHP cURL HEAD request

A HEAD request is a GET request without the body.

head_req.php

In order to generate a HEAD request, we set the CURLOPT_HEADERto true and the CURLOPT_NOBODY to false. We set all the options at once with curl_setopt_array.

PHP cURL status code

With the curl_getinfo function we get information regarding aspecific transfer.

We send a HEAD reqeust to a website. After executing the request, we get the status by passing the CURLINFO_RESPONSE_CODE option to the curl_getinfo function.

PHP cURL POST form

The POST form request issues a POST to the specified URL, with data's keys andvalues URL-encoded as the request body. The Content-Type header is set toapplication/x-www-form-urlencoded. The data is sent in the body of the request;the keys and values are encoded in key-value tuples separated by '&', with a '='between the key and the value.

The POST request is set with the CURLOPT_POST option. The POST fields are set with the CURLOPT_POSTFIELDS option.

PHP cURL POST JSON

Curlopt_postfields

In the following example, we POST JSON data.

post_json.php

We encode the JSON data with the json_encode function. We set the appropriate header with the CURLOPT_HTTPHEADER option.

PHP cURL multiple async requests

The curl_multi_init function creates a new multi handle, which allows the processing of multiple cURL handles asynchronously.

In the example, we create asynchronous requests to four websites. We print theirstatus codes and headers.

We initiate the multi handle.

We create standard handles for each URLs and add them to the multi handle withcurl_multi_add_handle.

We execute all queries asynchronously, and continue when all are complete.

We close the handles.

We get the status codes.

We get the headers.

PHP cURL send email

We build a custom request with the CURLOPT_CUSTOMREQUEST option.

send_mail

The example sends an email to a computer on a local network.

The core9 is the name of the computer running email server on a LAN.

We specify the port number with CURLOPT_PORT. TheCURLOPT_CRLF translates Unix new lines into rn,which are control characters of the SMTP protocol.

The mail is build by using the SMPT commands.

We check the email with an email client on the server.

In this tutorial, we have worked with the PHP cURL library. Php copy file.

List all PHP tutorials.

Acknowledgments

New to cURL? If yes, check out the following articles to learn the purposes and basics of cURL/libcurl.

Curlopt_post

Please note that some of the techniques shown here can be used for “blackhat” methods. The goal of this article is only educationnal, please do not use any of the snippets below for illegal stuff.

1 – Update your Facebook status

Wanna update your facebook status, but don’t want to go to facebook.com, login, and finally being able to update your status? Simply save the following code on your server, define the variables, and voilà !

Curlopt_post

2 – Get download speed of your webserver

Do you ever wanted to know the exact download speed of your webserver (or any other?) If yes, you’ll love that code. You just have to initialize the $url variable with any resources from the webserver (images, pdf, etc), place the file on your server and point your browser to it. The output will be a full report of download speed.

Source:https://cowburn.info/2008/11/29/download-speed-php-curl

3 – Myspace login using cURL

Source: http://www.seo-blackhat.com/article/myspace-login-function-php-curl.html

Curlopt_postfieldsize

4 – Publish a post on your WordPress blog, using cURL

This function can post on your WordPress blog. You don’t need to login to your WP dashboard etc.
Though, you must activate the XMLRPC posting option in your WordPress blog. If this option isn’t activated, the code will not be able to insert anything into WordPress database. Another thing, make sure the XMLRPC functions are activated on your php.ini file.

5 – Test the existence of a given url

I know, it sounds basic. In fact, it is basic, but it is also very useful, especially when you have to work with external resources.

Source: http://www.phpsnippets.info/test-existence-of-a-given-url-with-curl

6 – Post comments on WordPress blogs

In a previous article, I have discussed how spammers spams your WordPress blog. To do so, they simply have to fill the $postfields array with the info they want to display and load the page.
Of course, this code is only for educationnal purposes.

7 – Follow your Adsense earnings with an RSS reader

Most bloggers uses Adsense on their blog and (try to) make money with Google. This excellent snippet allows you to follow your Adsense earnings…with a RSS reader! Definitely awesome.
(Script too big to be displayed on the blog, click here to preview)
Source:http://planetozh.com/blog/my-projects/track-adsense-earnings-in-rss-feed/

8 – Get feed subscribers count in full text

If you’re a blogger, you’re probably using the popular FeedBurner service, which allo you to know how many people grabbed your rss feed. Feedburner have a chicklet to proudly display your subscriber count on your blog. I personally like the chicklet’s look, but I heard lots of bloggers complaining about it. happilly, cURL can simply grab the count value and return it to you as a variable so you can display it as you want on your blog.

Source:https://www.hongkiat.com/blog/display-google-feed-subscriber-count-in-text/

9 – Get the content of a webpage into a PHP variable

This is a very basic thing to do with cURL, but with endless possibilities. Once you have a webpage in a PHP variable, you can for example, retrieve a particular information on the page to use on your own website.

10 – Post to Twitter using PHP and cURL

Twitter is very popular since some time now, and you probably already have an account there. (We have one too) So, what about using cURL to tweet from your server without connectiong to Twitter?

Source:https://morethanseven.net/2007/01/20/posting-to-twitter-using-php/