Php curl post with parameters

Can anyone show me how khổng lồ vì chưng a php curl with an HTTP POST?

I want khổng lồ send data lượt thích this:

username=user1, password=passuser1, gender=1

To www.tên miền.com

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


"value1"))); // Receive server response ... curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $server_output = curl_exec($ch); curl_cthua ($ch); // Further processing ... if ($server_output == "OK") else ?>
Share
Follow
edited Aug 7 "18 at 7:50
mimarcel
696 7 7 silver badges 20 20 bronze badges
answered Jan 26 "10 at 9:40
mikumiku
159k 45 45 gold badges 282 282 silver badges 297 297 bronze badges
5
57
no need lớn use http_build_query() to lớn handle parameters; just pass the array lớn CURLOPT_POSTFIELDS is enough. – Raptor Apr 5 "16 at 3:13
10
Raptor providing array directly to 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
28
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 phản hồi  | 
283

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); // cthảm bại the connection, release resources used curl_close($ch); // vày 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 and let the class above implement it. Then you can always swap this implementation with another adapter of your like, without any side effects khổng lồ your application.

Bạn đang xem: Php curl post with parameters

Using HTTPS / encrypting traffic

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

Xem thêm: Sửa Lỗi Steam_Api.Dll - How To Fix Steam_Api64

What most people vày here is khổng lồ 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 persize various attacks on your phầm mềm lượt thích Man In The Middle attack or such.

Never, ever bởi that. Instead, you simply need to lớn modify your php.ini and tell PHP where your CA Certificate tệp tin is to let it verify certificates correctly:

; modify the absolute path khổng lồ the cacert.pem tệp tin curl.cainfo=c:phpcacert.pem

The lathử nghiệm cacert.pem can be downloaded from the Internet or extracted from your favorite browser. When changing any php.ini related settings remember to restart your webVPS.