php 發送url 方式

來源:互聯網
上載者:User

方法一:HTTP函數發送方式

說明:$data為POST發送的資料:$key為參數名,$val為參數值

[php]view plaincopy
  1. $URL = "http://pre.payment.sdoa.sdo.com/";

  2. $data = $key1."=".val1."&".$key2."=".val2;

  3. $PostResult = http_post_data($BGWURL,$data );


    方法二:Curl Post資料

    [php]view plaincopy
    1. function _curl_post($url, $vars) {

    2. $ch = curl_init();

    3. curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);

    4. curl_setopt($ch, CURLOPT_URL,$url);

    5. curl_setopt($ch, CURLOPT_POST, 1 );

    6. curl_setopt($ch, CURLOPT_HEADER, 0 ) ;

    7. curl_setopt($ch, CURLOPT_POSTFIELDS, "var=".$vars);

    8. $data = curl_exec($ch);

    9. curl_close($ch);

    10. if ($data)

    11. return$data;

    12. else

    13. return false;

    14. }


    方法三:fsockopen方式

    說明:$data為POST發送的資料:$data為數組形式

    [php]view plaincopy
    1. function posttohost($url, $data) {

    2. $url = parse_url($url);

    3. if (!$url) return"couldn't parse url";

    4. if (!isset($url['port'])) { $url['port'] = ""; }

    5. if (!isset($url['query'])) { $url['query'] = ""; }

    6. $encoded = "";

    7. while (list($k,$v) = each($data)) {

    8. $encoded .= ($encoded ? "&" : "");

    9. $encoded .= rawurlencode($k)."=".rawurlencode($v);

    10. }

    11. $fp = fsockopen($url['host'], $url['port'] ? $url['port'] : 80);

    12. if (!$fp) return"Failed to open socket to $url[host]";

    13. fputs($fp, sprintf("POST %s%s%s HTTP/1.0/n", $url['path'], $url['query'] ? "?" : "", $url['query']));

    14. fputs($fp, "Host: $url[host]/n");

    15. fputs($fp, "Content-type: application/x-www-form-urlencoded/n");

    16. fputs($fp, "Content-length: " . strlen($encoded) . "/n");

    17. fputs($fp, "Connection: close/n/n");

    18. fputs($fp, "$encoded/n");

    19. $line = fgets($fp,1024);

    20. if (!eregi("^HTTP/1/.. 200", $line)) return;

    21. $results = ""; $inheader = 1;

    22. while(!feof($fp)) {

    23. $line = fgets($fp,1024);

    24. if ($inheader && ($line == "/n" || $line == "/r/n")) {

    25. $inheader = 0;

    26. }

    27. elseif (!$inheader) {

    28. $results .= $line;

    29. }

    30. }

    31. fclose($fp);

    32. return$results;

    33. }



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.