使用php curl运行命令

时间:2013-12-17 18:56:42

标签: php curl

我如何运行这个突击队员卷曲:

curl -F“param01 = value01”-F“param02 = value02”-v http://example.com/Home/Login

使用PHP?

怀疑是因为参数-F,我从未使用过它......

2 个答案:

答案 0 :(得分:2)

<强>更新

curl man page:

  

-F, - form

     

(HTTP)这让curl模拟用户拥有的填充表单   按下提交按钮。这会导致curl使用   根据RFC 2388,Content-Type multipart / form-data。这样就可以了   上传二进制文件等。强制'内容'部分为   文件,用@符号作为文件名的前缀。要获得内容   部分来自文件,文件名前缀为符号&lt;。该   @和&lt;之间的差异然后是@使文件被附加   该帖子作为文件上传,而&lt;创建一个文本字段,然后得到   文件中该文本字段的内容。

您将在我的回复底部使用POST示例。

如果param01和param02是GET / url参数,这将有效。

<?php

// Setup our curl handler
if (!$ch = curl_init())
{
    die("cURL package not installed in PHP");
}

$value1 = urlencode("something");
$value2 = urlencode("something");

curl_setopt($ch, CURLOPT_URL,'http://example.com/Home/Login?param01='.$value1.'&param02='.$value2);
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE); // TRUE if we want to track the request string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // TRUE to return the transfer as a string

$response = curl_exec($ch);

if(curl_error($ch) != "")
{
    die("Error with cURL installation: " . curl_error($ch));
}
else
{
    // Do something with the response
    echo $response;
}
curl_close($ch);

如果他们是POST(表单数据):

<?php

// Setup our curl handler
if (!$ch = curl_init())
{
    die("cURL package not installed in PHP");
}

$value1 = urlencode("something");
$value2 = urlencode("something");

$data = array(
    'param1' => $value1,
    'param1' => $value2,
)

curl_setopt($ch, CURLOPT_URL,'http://example.com/Home/Login');
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE); // TRUE if we want to track the request string
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // TRUE to return the transfer as a string
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$response = curl_exec($ch);

if(curl_error($ch) != "")
{
    die("Error with cURL installation: " . curl_error($ch));
}
else
{
    // Do something with the response
    echo $response;
}
curl_close($ch);

答案 1 :(得分:0)

您需要的一切以及更多:

http://php.net/curl

简单的例子:

$ch = curl_init();
$curlConfig = array(
    CURLOPT_URL            => "http://www.example.com/yourscript.php",
    CURLOPT_POST           => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS     => array(
        'field1' => 'some date',
        'field2' => 'some other data',
    ),
);
curl_setopt_array($ch, $curlConfig)
$result = curl_exec($ch);
curl_close($ch);