簡體   English   中英

在curl php中請求帶有參數的URL

[英]request URL with parameters in curl php

我想要從下拉列表中選擇特定選項的特定網頁內容。

在我的示例中,我想要來自網頁的內容,其中社區和級別是兩個下拉列表。 我想要選項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?

您需要將cURL POST參數設置為true

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
));

根據手冊CURLOPT_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
    ));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM