简体   繁体   English

CURL的HTTP发布-PHP

[英]HTTP post by CURL - PHP

I am trying to send HTTP data with CURL in PHP file , the code not send the data. 我正在尝试用PHP文件中的CURL发送HTTP数据,代码不发送数据。

$url= 'http://www.example.com/test.php?' ;
$msg= 'p1=1234&p2=1234&p3=1234' ;

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL,$url);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $msg);

    curl_exec ($curl);
    curl_close ($curl);

You need to send post fields as an array, not as a 'GET' request string: 您需要将帖子字段作为数组发送,而不是作为“ GET”请求字符串发送:

$msg = [
 'p1' => '1234',
 'p2' => '1234',
 'p3' => '1234',
];

If you want to send this as a one message you can send an array with one value: 如果要将此消息作为一个消息发送,则可以发送一个具有一个值的数组:

$msg = ['msg' => 'some_message'];

If you want to send GET request, append your query string to the URL: 如果要发送GET请求,请将查询字符串附加到URL:

curl_setopt($curl, CURLOPT_URL, $url.$msg);

In the future you can get debugging info by using this code: 将来,您可以使用以下代码获取调试信息:

$result = curl_exec($curl);
echo 'curl_error: ';
var_dump(curl_error($curl));
echo '<br /><br /> curl_result: ';
var_dump($result);
curl_close($curl);

Please can you try the code below ? 请您尝试下面的代码吗?

$requestBody can be an array, querystring, json or else according to your request headers and the request handler which will recieve your http request.

contents of the test-curl.php:

<?PHP
error_reporting();
ini_set('display_errors', 'On');

function makeRequest($url, $requestBody)
{
    $handle = curl_init();
    $headers = array(); //array of request headers
    //Example headers for standart browser request
    //$headers = array(
    //   'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');

    curl_setopt($handle, CURLOPT_URL, $url);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($handle, CURLOPT_POST, 1);
    curl_setopt($handle, CURLOPT_POSTFIELDS, $requestBody);
    curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($handle, CURLOPT_HEADER, 1); //This means include headers in response

    $result = curl_exec($handle);
    $header_size = curl_getinfo($handle, CURLINFO_HEADER_SIZE);
    $responseHeaders = substr($result, 0, $header_size);
    $responseBody = substr($result, $header_size, strlen($result) - $header_size);
    $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
    if ($httpCode == 200)
        return $responseBody;
    else
        throw new Exception($responseBody);
}

$method = $_SERVER["REQUEST_METHOD"];

if ($method == "GET") {

    $request = array();
    $request["a"] = "1";
    $request["b"] = "3";
    $request["c"] = "4";
    $request["d"] = "7";
    //$request = "a=1&b=3&c=4&d=7"; //This is the same with array version for standart post request.
    $response = makeRequest("http://localhost/test-curl.php", $request);
    echo $response;
} else {
    print_r($_POST);
}
?>

If you run your test-curl.php with http://localhost/test-curl.php url it will make a post request to itself and you will see the print_r output of $_POST array. 如果使用http://localhost/test-curl.php url运行test-curl.php,它将向自身发出发布请求,并且您将看到$ _POST数组的print_r输出。 Something like below; 像下面这样:

Array ( [a] => 1 [b] => 3 [c] => 4 [d] => 7 ) 数组([a] => 1 [b] => 3 [c] => 4 [d] => 7)

Hope this helps you. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM