The following code to introduce the PHP Curl simulated POST request sample code, the following code:
<?php
$uri = "http://www.cnblogs.com/test.php";//here Replace the address
//parameter array of your own server
$data = array (
' name ' = > ' Tanteng '
//' password ' => ' password '
);
$ch = Curl_init ();
Print_r ($ch);
curl_setopt ($ch, Curlopt_url, $uri);
curl_setopt ($ch, Curlopt_post, 1);
curl_setopt ($ch, Curlopt_header, 0);
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_postfields, $data);
$return = curl_exec ($ch);
Curl_close ($ch);
Print_r ($return);
2, remote server:
<?php
if (isset ($_post[' name ')) {
if (!empty ($_post[' name ')) {
echo ' Hello, ', $_post[' name ']. ' ';
}
}
Here's a description of the PHP Curl analog post submitted multidimensional array.
Today, we need to use curl to simulate post submission parameters, request a colleague to provide an interface, but in the parameters passed, there is a parameter of the value of the array, with a normal curl post code submitted, will report an error
PHP Notice:array to string conversion in/test/functions.php in line 30
Notice:array to string conversion in/test/functions.php on line 30
The code is as follows:
<?php
$param = Array (
' uid ' => 123,
' UIDs ' => Array (12,455),
' Msgtype ' => ' with ',
' Nick ' => ' AAA ',
);
$url = "http://cx.com/t.php";
Sending an interface request
Senddatabycurl ($url, $param) via Curl post mode;
A request to simulate a post via Curl
, function Senddatabycurl ($url, $data =array ()) {
//to escape a space
$url = Str_replace (', ' + ', $url);
$ch = Curl_init ();
Set options, including URL
curl_setopt ($ch, Curlopt_url, "$url");
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_header, 0);
curl_setopt ($ch, curlopt_timeout,3); Definition Timeout 3 seconds
//POST data
curl_setopt ($ch, Curlopt_post, 1);
Add the post variable to the
curl_setopt ($ch, Curlopt_postfields, $data);
Executes and obtains the content of the URL address
$output = curl_exec ($ch);
Release Curl handle
curl_close ($ch);
return $output;
}
After modifying the above code, you can complete the function of submitting the array without reporting PHP notice, the code is as follows: