Passing $_POST values with cURL

后端 未结 8 1602
野的像风
野的像风 2020-11-22 16:21

How do you pass $_POST values to a page using cURL?

8条回答
  •  遇见更好的自我
    2020-11-22 16:57

    Ross has the right idea for POSTing the usual parameter/value format to a url.

    I recently ran into a situation where I needed to POST some XML as Content-Type "text/xml" without any parameter pairs so here's how you do that:

    $xml = 'foobar';
    $httpRequest = curl_init();
    
    curl_setopt($httpRequest, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($httpRequest, CURLOPT_HTTPHEADER, array("Content-Type:  text/xml"));
    curl_setopt($httpRequest, CURLOPT_POST, 1);
    curl_setopt($httpRequest, CURLOPT_HEADER, 1);
    
    curl_setopt($httpRequest, CURLOPT_URL, $url);
    curl_setopt($httpRequest, CURLOPT_POSTFIELDS, $xml);
    
    $returnHeader = curl_exec($httpRequest);
    curl_close($httpRequest);
    

    In my case, I needed to parse some values out of the HTTP response header so you may not necessarily need to set CURLOPT_RETURNTRANSFER or CURLOPT_HEADER.

提交回复
热议问题