【问题标题】:request URL with parameters in curl phpcurl php中带有参数的请求URL
【发布时间】:2014-03-06 12:09:14
【问题描述】:

我想为从下拉列表中选择的特定选项请求特定网页内容。

在我的示例中,我想要社区和级别是两个下拉列表的网页中的内容。我想要选项Community='SoftwareFactory/Cloude'Level='V6R2015x' 的网页。

我的代码是

<?php
// init the resource
$ch = curl_init('http://e4allds/');

// set a single option...
$postData = array(
    'Community' => 'SoftwareFactory/Cloud',
    'Level' => 'V6R2015x'
);
curl_setopt_array(
    $ch, array( 
    CURLOPT_URL => 'http://e4allds/',
    CURLOPT_POSTFIELDS => $postData,
    //OPTION1=> 'Community=SOftwareFactory/Cloud',
    //OPTION2=> 'Level=V6R2015x',
    CURLOPT_RETURNTRANSFER => true
));

$output = curl_exec($ch);
echo $output;

但它给出了默认选择的结果。谁能帮助我如何将这些参数传递给 URL?

【问题讨论】:

    标签: php curl


    【解决方案1】:

    您需要为true启用cURL POST参数。

    curl_setopt_array(
        $ch, array( 
        CURLOPT_POST => true, //<------------ This one !
        CURLOPT_URL => 'http://e4allds/',
        CURLOPT_POSTFIELDS => $postData,
        //OPTION1=> 'Community=SOftwareFactory/Cloud',
        //OPTION2=> 'Level=V6R2015x',
        CURLOPT_RETURNTRANSFER => true
    ));
    

    【讨论】:

      【解决方案2】:

      根据manualCURLOPT_POSTFIELDS 选项是要在HTTP "POST" 操作中发布的完整数据

      所以,要么你应该切换到POST 方法:

      curl_setopt_array(
        $ch, array( 
          CURLOPT_URL => 'http://e4allds/',
          CURLOPT_POST => true,
          CURLOPT_POSTFIELDS => $postData,
          CURLOPT_RETURNTRANSFER => true
          ));
      

      或者,如果您希望继续使用GET 方法,请将所有参数放在查询字符串中:

      curl_setopt_array(
        $ch, array( 
          CURLOPT_URL => 'http://e4allds/?' . http_build_query($postData,null,'&'),
          CURLOPT_RETURNTRANSFER => true
          ));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-30
        • 1970-01-01
        相关资源
        最近更新 更多