繁体   English   中英

在PHP中使用CURL发出PUT请求

[英]Making a PUT request using CURL in PHP

我正在使用PHP 5.3.6,看来我无法使用CURL进行PUT要求以仅将字符串字符串进行放置。

function put_data($url, $data)
{   
  $useragent="SimpleAgent-1.0";
  $fh = fopen('php://memory', 'rw');
  fwrite($fh, $data);
  rewind($fh);$ch = curl_init();
  curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
  curl_setopt($ch, CURLOPT_INFILE, $fh);
  curl_setopt($ch, CURLOPT_INFILESIZE, strlen($data));
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  curl_setopt($ch, CURLOPT_PUT, 1);
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $result = curl_exec($ch);
  curl_close($ch);
  fclose($fh);

  return $result;
}

在这里,$ data是我要放入的字符串。 不起作用,并返回以下错误:

500内部服务器错误服务器已错误或无法执行请求的操作。

预期的字符串或缓冲区

我只能使用所使用的版本将数组作为数据传递。 所以这就是我现在正在做的:

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($ch, CURLOPT_COOKIE, $curl_cookie);
$arr = array();
if (isset($data)) {
    $arr['my_data'] = $data;
}

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($arr));
curl_exec($ch);

我使用了您的代码,因此定义了一个url,并用字符串填充了数据,并且所有工作均按预期进行。 我被网站拒绝了,因为没有接收端可以处理看跌期权。 要轻松获取信息,只需添加以下行: curl_setopt($ch, CURLOPT_VERBOSE, true);

你会得到类似的东西:

* About to connect() to yyyy.xxxx.com port 80 (#0)
*   Trying 62.221.196.28...
* connected
* Connected to yyyy.xxxx.com (zz.221.196.28) port 80 (#0)
> PUT / HTTP/1.1
User-Agent: SimpleAgent-1.0
Host: yyyy.xxxx.com
Accept: */*
Content-Length: 14
Expect: 100-continue

< HTTP/1.1 100 Continue
* We are completely uploaded and fine
< HTTP/1.1 405 Method Not Allowed
< Date: Thu, 09 Feb 2012 19:46:28 GMT
< Server: Apache
< Allow: GET,HEAD,POST,OPTIONS
< Vary: Accept-Encoding
< Content-Length: 231
< Content-Type: text/html; charset=iso-8859-1
< 
* Connection #0 to host yyy.xxxx.com left intact
* Closing connection #0

从日志记录中可以看到,请求已发出,但是当您要放置数据时,apache设置不允许您将数据放置到该url中。 因此,根据服务器的不同,您将必须处理一个接受PUT的接收URL。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM