如何使用PHP发送OPTIONS请求 [英] How to send an OPTIONS request using PHP
本文介绍了如何使用PHP发送OPTIONS请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人知道如何使用PHP发送 OPTIONS请求。
我找不到执行此操作的curl setopt。
我正在使用php 5.6.7
我已经弄清楚了GET,POST,DELETE和PUT。只是需要选项。
我尝试了以下高清答案:
$ ch = curl_init();
curl_setopt($ ch,CURLOPT_URL, theurl);
curl_setopt($ ch,CURLOPT_CUSTOMREQUEST, OPTIONS);
$ r = curl_exec($ ch);
print_r($ http_response_header);
curl_close($ ch);
而我得到了错误:
未定义变量:C:\IIS_Emea\WebRoot\Site01\test\rest1.php,位于第7行
<中的http_response_header / pre>
如何获取结果?
解决方案
<?php
$ ch = curl_init();
curl_setopt_array($ ch,array(
CURLOPT_URL =>'http://stackoverflow.com/',
CURLOPT_CUSTOMREQUEST =>'OPTIONS',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_NOBODY => true,
CURLOPT_VERBOSE => true,
)));
$ r = curl_exec($ ch);
回显PHP_EOL。’响应标题:’。PHP_EOL;
print_r($ r);
curl_close($ ch);
功能:
CURLOPT_CUSTOMREQUEST => OPTIONS
-定义HTTP请求方法。
CURLOPT_RETURNTRANSFER => true
-告诉curl_exec
不要输出结果,而是返回结果。
CURLOPT_HEADER => true
-返回标头。
CURLOPT_NOBODY => true
-不返回正文。
CURLOPT_VERBOSE => true
-仅用于调试,请在生产时将其删除。它允许查看由库完成的请求和收到的响应。
脚本的输出如下所示:
*正在尝试104.16.36.249 ...
*已连接到stackoverflow.com(104.16.36.249)端口80(#0)
>选项/ HTTP / 1.1
主机:stackoverflow.com
接受:* / *
< HTTP / 1.1 200 OK
<日期:2016年4月20日,星期三09:02:44 GMT
<内容类型:text / html; charset = utf-8
<传输编码:大块
<连接:保持活动
< Set-Cookie:__cfduid = d96e454843a81721eeb77cc4ebb49d2c91461142964; expires =星期四,17年4月20日09:02:44 GMT;路径= /; domain = .stackoverflow.com; HttpOnly
<缓存控制:public,no-cache = Set-Cookie,max-age = 60
<过期:2016年4月20日星期三09:03:44 GMT
<上次修改时间:2016年4月20日,星期三09:02:44 GMT
<变化:*
< X-Frame-Options:SAMEORIGIN
< X-Request-Guid:a2f90416-9ec1-4ec9-b2d9-a69e22cc05d5
<套餐Cookie:prov = 37be6386-6390-40fb-9f0a-42efdfcf2d71; domain = .stackoverflow.com; expires =星期五,格林尼治标准时间2055年1月1日00:00:00;路径= /; HttpOnly
<服务器:cloudflare-nginx
< CF-RAY:29676b4517f92b21-WAW
<
*在非流水线读取中发现超额:超额= 725 url = /(零长度正文)
*与主机stackoverflow.com的连接#0保持不变
响应标头:
HTTP / 1.1 200 OK
日期:2016年4月20日星期三09:02:44 GMT
内容类型:text / html; charset = utf-8
传输编码:分块
连接:keep-alive
Set-Cookie:__cfduid = d96e454843a81721eeb77cc4ebb49d2c91461142964; expires =星期四,17年4月20日09:02:44 GMT;路径= /; domain = .stackoverflow.com; HttpOnly
Cache-Control:public,no-cache = Set-Cookie,max-age = 60
过期:2016年4月20日,星期三09:03:44 GMT
最后修改时间:2016年4月20日,星期三09:02:44 GMT
更改:*
X帧选项:SAMEORIGIN
X-Request-Guid:a2f90416-9ec1-4ec9-b2d9-a69e22cc05d5
Set-Cookie:prov = 37be6386-6390-40fb-9f0a-42efdfcf2d71; domain = .stackoverflow.com; expires =星期五,格林尼治标准时间2055年1月1日00:00:00;路径= /; HttpOnly
服务器:cloudflare-nginx
CF-RAY:29676b4517f92b21-WAW
您将获得响应标头作为字符串,并且需要对其进行解析。但是我想这超出了问题的范围。
Does anyone know how to send an "OPTIONS" request using PHP.
I can't find a curl setopt that does this.
I'm using php 5.6.7
I've figured out GET, POST, DELETE, and PUT. Just need OPTIONS.
I have tried hd's answer below:
$ch = curl_init(); curl_setopt($ch,CURLOPT_URL,"theurl"); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "OPTIONS"); $r = curl_exec($ch); print_r($http_response_header); curl_close($ch);
and I'm getting the error:
Undefined variable: http_response_header in C:\IIS_Emea\WebRoot\Site01\test\rest1.php on line 7
How do I get the results?
解决方案<?php $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_URL => 'http://stackoverflow.com/', CURLOPT_CUSTOMREQUEST => 'OPTIONS', CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, CURLOPT_NOBODY => true, CURLOPT_VERBOSE => true, )); $r = curl_exec($ch); echo PHP_EOL.'Response Headers:'.PHP_EOL; print_r($r); curl_close($ch);
What it does:
CURLOPT_CUSTOMREQUEST => 'OPTIONS'
- defines the HTTP request method.CURLOPT_RETURNTRANSFER => true
- tellcurl_exec
not to output results but return them.CURLOPT_HEADER => true
- return headers.CURLOPT_NOBODY => true
- do not return body.CURLOPT_VERBOSE => true
- just for debugging, remove it on production. It allows to see the request done by the library and the response received.The output from the script looks like this
* Trying 104.16.36.249... * Connected to stackoverflow.com (104.16.36.249) port 80 (#0) > OPTIONS / HTTP/1.1 Host: stackoverflow.com Accept: */* < HTTP/1.1 200 OK < Date: Wed, 20 Apr 2016 09:02:44 GMT < Content-Type: text/html; charset=utf-8 < Transfer-Encoding: chunked < Connection: keep-alive < Set-Cookie: __cfduid=d96e454843a81721eeb77cc4ebb49d2c91461142964; expires=Thu, 20-Apr-17 09:02:44 GMT; path=/; domain=.stackoverflow.com; HttpOnly < Cache-Control: public, no-cache="Set-Cookie", max-age=60 < Expires: Wed, 20 Apr 2016 09:03:44 GMT < Last-Modified: Wed, 20 Apr 2016 09:02:44 GMT < Vary: * < X-Frame-Options: SAMEORIGIN < X-Request-Guid: a2f90416-9ec1-4ec9-b2d9-a69e22cc05d5 < Set-Cookie: prov=37be6386-6390-40fb-9f0a-42efdfcf2d71; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly < Server: cloudflare-nginx < CF-RAY: 29676b4517f92b21-WAW < * Excess found in a non pipelined read: excess = 725 url = / (zero-length body) * Connection #0 to host stackoverflow.com left intact Response Headers: HTTP/1.1 200 OK Date: Wed, 20 Apr 2016 09:02:44 GMT Content-Type: text/html; charset=utf-8 Transfer-Encoding: chunked Connection: keep-alive Set-Cookie: __cfduid=d96e454843a81721eeb77cc4ebb49d2c91461142964; expires=Thu, 20-Apr-17 09:02:44 GMT; path=/; domain=.stackoverflow.com; HttpOnly Cache-Control: public, no-cache="Set-Cookie", max-age=60 Expires: Wed, 20 Apr 2016 09:03:44 GMT Last-Modified: Wed, 20 Apr 2016 09:02:44 GMT Vary: * X-Frame-Options: SAMEORIGIN X-Request-Guid: a2f90416-9ec1-4ec9-b2d9-a69e22cc05d5 Set-Cookie: prov=37be6386-6390-40fb-9f0a-42efdfcf2d71; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly Server: cloudflare-nginx CF-RAY: 29676b4517f92b21-WAW
You get response headers as a string and you need to parse it. But I guess this is out of scope of the question.
这篇关于如何使用PHP发送OPTIONS请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文