2011-12-21 75 views
3

考慮到我不想使用curl,這是發佈帖子請求的有效選擇嗎?也許Zend_http_client如何發出無捲髮的帖子請求?

我只需要基本的東西(我需要只有一個職位參數去請求URL)

+0

你寧願部署框架比使用cURL? – 2011-12-21 23:38:26

+0

@webarto:deploy? Zend框架只需要一個包含路徑。有了這個,你可以訪問這麼多的價值,它是值得的 – dynamic 2011-12-22 08:23:53

回答

0

如果你使用Zend框架已經你應該嘗試Zend_Http_Client你所提到的:

$client = new Zend_Http_Client($host, array(
      'maxredirects' => 3, 
      'timeout'  => 30)); 
$client->setMethod(Zend_Http_Client::POST); 
// You might need to set some headers here 
$client->setParameterPost('key', 'value'); 
$response = $client->request(); 
+0

不,我不使用zend框架,但考慮到這一點,我應該現在開始:D – dynamic 2011-12-21 21:25:16

+0

可能想檢查,但Zend可能只是捲曲 – Ascherer 2011-12-21 21:29:59

+0

無論如何如果我只想在我的框架中使用zend_http_client,是否有安裝教程?或者使用zend_http_client我必須使用所有的框架? – dynamic 2011-12-21 21:36:28

1

您可以通過套接字實現它自己:

$url = parse_url(''); // url 
    $requestArray = array('var' => 'value'); 
    $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); 
    socket_connect($sock, $url['host'], ((isset($url['port'])) ? $url['port'] : 80)); 
    if (!$sock) { 
     throw new Exception('Connection could not be established'); 
    } 

    $request = ''; 
    if (!empty($requestArray)) { 
     foreach ($requestArray as $k => $v) { 
      if (is_array($v)) { 
       foreach($v as $v2) { 
        $request .= urlencode($k).'[]='.urlencode($v2).'&'; 
       } 
      } 
      else { 
       $request .= urlencode($k).'='.urlencode($v).'&'; 
      } 
     } 
     $request = substr($request,0,-1); 
    } 
    $data = "POST ".$url['path'].((!empty($url['query'])) ? '?'.$url['query'] : '')." HTTP/1.0\r\n" 
    ."Host: ".$url['host']."\r\n" 
    ."Content-type: application/x-www-form-urlencoded\r\n" 
    ."User-Agent: PHP\r\n" 
    ."Content-length: ".strlen($request)."\r\n" 
    ."Connection: close\r\n\r\n" 
    .$request."\r\n\r\n"; 
    socket_send($sock, $data, strlen($data), 0); 

    $result = ''; 
    do { 
     $piece = socket_read($sock, 1024); 
     $result .= $piece; 
    } 
    while($piece != ''); 
    socket_close($sock); 
    // TODO: Add Header Validation for 404, 403, 401, 500 etc. 
    echo $result; 

當然,你必須改變它來滿足你的需求或將其包裝到一個函數中。

22

你可以使用file_get_contents()。

PHP手冊有一個不錯的example here。這僅僅是從手動複製過去:

$postdata = http_build_query(
    array(
     'var1' => 'some content', 
     'var2' => 'doh' 
    ) 
); 
$opts = array('http' => 
    array(
     'method' => 'POST', 
     'header' => 'Content-type: application/x-www-form-urlencoded', 
     'content' => $postdata 
    ) 
); 
$context = stream_context_create($opts); 
$result = file_get_contents('http://example.com/submit.php', false, $context); 
+0

超級簡單的方法來完成工作!謝謝。 – 2017-10-31 09:48:34

1

可以使用stream_context_createfile_get_contents

<?php 
$context_options = array (
     'http' => array (
      'method' => 'POST', 
      'header'=> "Content-type: application/x-www-form-urlencoded\r\n" 
       . "Content-Length: " . strlen($data) . "\r\n", 
      'content' => $data 
      ) 
     ); 
?> 


$context = stream_context_create($context_options); 
$data = file_get_contents('http://www.php.net', false, $context);