2011-12-21 56 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);