SEND REQUEST PHP CURL

Can anyone show me how to lớn bởi a php curl with an HTTP POST?

I want lớn skết thúc data like this:

username=user1, password=passuser1, gender=1

To www.tên miền.com

I expect the curl to lớn return a response like result=OK. Are there any examples?


"value1"))); // Receive sầu VPS response ... curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $server_output = curl_exec($ch); curl_cthất bại ($ch); // Further processing ... if ($server_output == "OK") else ?>
Share
Improve sầu this answer
Follow
edited Aug 7 "18 at 7:50
mimarcel
696 7 7 silver badges đôi mươi trăng tròn bronze badges
answered Jan 26 "10 at 9:40
mikumiku
159k 45 45 gold badges 283 283 silver badges 297 297 bronze badges
5
57
no need to use http_build_query() khổng lồ handle parameters; just pass the array to lớn CURLOPT_POSTFIELDS is enough. – Raptor Atruyền thông quảng cáo 5 "16 at 3:13
10
Raptor providing array directly to lớn CURLOPT_POSTFIELDS actually curl makes slightly different type of POST. (Expect: 100-continue) – Oleg Popov Atruyền thông quảng cáo 14 "16 at 4:49
29
Also if value of CURLOPT_POSTFIELDS is an array, the Content-Type header will be phối lớn multipart/form-data instead of application/x-www-form-urlencoded. php.net/manual/en/function.curl-setopt.php – Chloe Jul 21 "16 at 2:34
3
Using CURLOPT_RETURNTRANSFER means that curl_exec will return the response as a string rather than outputting it. – bnp887 Dec 28 "16 at 14:27
8
I suggest using true instead of 1 for CURLOPT_POST. – FluorescentGreen5 Aug 19 "17 at 8:53
Add a comment  | 
286

Procedural

// set post fields $post = < "username" => "user1", "password" => "passuser1", "gender" => 1, >; $ch = curl_init("http://www.example.com"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); // execute! $response = curl_exec($ch); // cđại bại the connection, release resources used curl_close($ch); // vì anything you want with your response var_dump($response);

Object oriented

return string *
throws RuntimeException On cURL error */ public function __invoke(array $post) curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); $response = curl_exec($ch); $error = curl_error($ch); $errno = curl_errno($ch); if (is_resource($ch)) if (0 !== $errno) return $response; } }

Usage

// create curl object $curl = new MyAppHttpCurlPost("http://www.example.com"); try catch (RuntimeException $ex)

Side note here: it would be best lớn create some kind of interface called AdapterInterface for example with getResponse() method & let the class above sầu implement it. Then you can always swap this implementation with another adapter of your lượt thích, without any side effects to your application.

Bạn đang xem: Send request php curl

Using HTTPS / encrypting traffic

Usually there"s a problem with cURL in PHPhường under the Windows operating system. While trying to lớn connect to lớn a https protected endpoint, you will get an error telling you that certificate verify failed.

Xem thêm: Xin Hỏi Phần Mềm Chia File Txt, Phần Mềm Cắt File Txt

What most people do here is to tell the cURL library khổng lồ simply ignore certificate errors and continue (curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);). As this will make your code work, you introduce huge security hole và enable malicious users to lớn perkhung various attacks on your app like Man In The Middle attachồng or such.

Never, ever vị that. Instead, you simply need to lớn modify your php.ini và tell PHPhường where your CA Certificate file is lớn let it verify certificates correctly:

; modify the absolute path to the cacert.pem file curl.cainfo=c:phpcacert.pem

The lakiểm tra cacert.pem can be downloaded from the Internet or extracted from your favorite browser. When changing any php.ini related settings rethành viên to restart your webserver.