如何将linux命令CURL转换为PHP

时间:2014-02-06 11:44:41

标签: php linux curl

我想将这个linux命令控制台转换为PHP代码,通过curl发送数据,

curl -X POST -d 'data[][street]=1' link.....

谢谢!

3 个答案:

答案 0 :(得分:2)

$fields = 2;
$fields_as_string = "key=value&key2=value"    

//open the curl connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
// set the number of post fields
curl_setopt($ch,CURLOPT_POST, $fields);
// set the fields
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_as_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

不要忘记确保你已经启用了curl php扩展。

答案 1 :(得分:1)

您可以使用PHP cURL Library.

    <?php
    //initializing the connection.
    $connection = curl_init();

    curl_setopt($connection, CURLOPT_URL,"http://www.yoursite.com");
    curl_setopt($connection, CURLOPT_POST, 1);
    curl_setopt($connection, CURLOPT_POSTFIELDS,http_build_query(array('postvar1' => 'value1')));

    // receive server response ...
    curl_setopt($connection, CURLOPT_RETURNTRANSFER, true);

    $server_output = curl_exec ($connection);

    curl_close ($connection);

    ?>

答案 2 :(得分:0)

您是否尝试过PHP cURL库? ;)

https://php.net/curl

PHP文档总是提供很好的例子。