我正在尝试使curl遵循重定向,但我不能让它正常工作.我有一个字符串,我想作为GET参数发送到服务器并获取结果URL.
例:
String = Kobold Vermin
Url = www.wowhead.com/search?q=Kobold+Worker
如果您转到该网址,它会将您重定向到"www.wowhead.com/npc=257".我希望curl将此URL返回到我的PHP代码,以便我可以提取"npc = 257"并使用它.
当前代码:
function npcID($name) {
$urltopost = "http://www.wowhead.com/search?q=" . $name;
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
curl_setopt($ch, CURLOPT_URL, $urltopost);
curl_setopt($ch, CURLOPT_REFERER, "http://www.wowhead.com");
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type:application/x-www-form-urlencoded"));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
return curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
}
Run Code Online (Sandbox Code Playgroud)
然而,这将返回www.wowhead.com/search?q=Kobold+Worker而不是www.wowhead.com/npc=257.
我怀疑在外部重定向发生之前PHP正在返回.我怎样才能解决这个问题?
当我回音时,我得到了这个,当我将它输入终端时运行
curl -i \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
-X POST --data '{"account":{"email":"akdgdtk@test.com","screenName":"akdgdtk","type":"NIKE","passwordSettings":{"password":"Starwars1","passwordConfirm":"Starwars1"}},"firstName":"Test","lastName":"User","middleName":"ObiWan","locale":"en_US","registrationSiteId":"520","receiveEmail":"false","dateOfBirth":"1984-12-25","mobileNumber":"9175555555","gender":"male","fuelActivationDate":"2010-10-22","postalCode":"10022","country":"US","city":"Beverton","state":"OR","bio":"This is a test user","jpFirstNameKana":"unsure","jpLastNameKana":"ofthis","height":"80","weight":"175","distanceUnit":"MILES","weightUnit":"POUNDS","heightUnit":"FT/INCHES"}' https://xxx:xxxxx@xxxx-www.xxxxx.com/xxxxx/xxxx/xxxx
Run Code Online (Sandbox Code Playgroud)
但是当在bash脚本文件中运行时,我收到此错误
curl: (6) Could not resolve host: application; nodename nor servname provided, or not known
curl: (6) Could not resolve host: is; nodename nor servname provided, or not known
curl: (6) Could not resolve host: a; nodename nor servname provided, or not known
curl: (6) Could not resolve host: test; nodename nor servname provided, or not known
curl: (3) [globbing] …Run Code Online (Sandbox Code Playgroud) 我试图在cygwin上启用curl,但它说 bash: curl: command not found
如何在cygwin上安装curl?
我使用这种语法发布文件以及一些参数:
curl -v -include --form "key1=value1" --form upload=localfilename URL
Run Code Online (Sandbox Code Playgroud)
该文件大小约为500K.首先,我看到发送端的内容长度为254.之后服务器响应的内容长度为0.我哪里错了?
这是命令的完整跟踪.
* Couldn't find host xxx.xxx.xxx.xxx in the _netrc file; using defaults
* About to connect() to xxx.xxx.xxx.xxx port yyyy (#0)
* Trying xxx.xxx.xxx.xxx...
* Adding handle: conn: 0x4b96a0
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x4b96a0) send_pipe: 1, recv_pipe: 0
* Connected to xxx.xxx.xxx.xxx (xxx.xxx.xxx.xxx) port yyyy (#0)
* POST /zzzzzz/UploadFile HTTP/1.1
* User-Agent: curl/7.32.0
* Host: xxx.xxx.xxx.xxx:yyyy …Run Code Online (Sandbox Code Playgroud) 我正在寻找使用Node.js的HTTP请求中的信息(即调用远程Web服务并回应客户端的响应).
在PHP中我会用cURL来做这件事.Node中的最佳实践是什么?
我想在python中执行curl命令.
通常,我只需要在终端输入命令并按回车键.但是,我不知道它在python中是如何工作的.
该命令如下所示:
curl -d @request.json --header "Content-Type: application/json" https://www.googleapis.com/qpxExpress/v1/trips/search?key=mykeyhere
Run Code Online (Sandbox Code Playgroud)
有一个request.json文件要发送以获得响应.
我经常搜索并感到困惑.我试着写一段代码,虽然我无法完全理解.它没用.
import pycurl
import StringIO
response = StringIO.StringIO()
c = pycurl.Curl()
c.setopt(c.URL, 'https://www.googleapis.com/qpxExpress/v1/trips/search?key=mykeyhere')
c.setopt(c.WRITEFUNCTION, response.write)
c.setopt(c.HTTPHEADER, ['Content-Type: application/json','Accept-Charset: UTF-8'])
c.setopt(c.POSTFIELDS, '@request.json')
c.perform()
c.close()
print response.getvalue()
response.close()
Run Code Online (Sandbox Code Playgroud)
错误信息是'Parse Error'.任何人都可以告诉我如何修复它?或者如何正确地从服务器获得响应?
我正在使用PHP curl函数从我的本地计算机将数据发布到Web服务器.我的代码如下:
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($c);
if (curl_exec($c) === false) {
echo "ok";
} else {
echo "error";
}
curl_close($c);
Run Code Online (Sandbox Code Playgroud)
不幸的是,我无法捕获404,500或网络故障等任何错误.那么我怎样才能知道数据没有发布到遥控器或从遥控器中检索?
它们是否相同?某些事情可以用一个而不是另一个完成吗?那些是什么?或者,在一天结束时,是熟悉的问题?
我编写了一个bash脚本,它使用curl从一个网站输出,并在html输出上做了一堆字符串操作.问题是当我针对返回其输出gzip的网站运行它时.在浏览器中访问该网站工作正常.
当我手动运行curl时,我得到gzipped输出:
$ curl "http://example.com"
Run Code Online (Sandbox Code Playgroud)
这是该特定网站的标题:
HTTP/1.1 200 OK
Server: nginx
Content-Type: text/html; charset=utf-8
X-Powered-By: PHP/5.2.17
Last-Modified: Sat, 03 Dec 2011 00:07:57 GMT
ETag: "6c38e1154f32dbd9ba211db8ad189b27"
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Cache-Control: must-revalidate
Content-Encoding: gzip
Content-Length: 7796
Date: Sat, 03 Dec 2011 00:46:22 GMT
X-Varnish: 1509870407 1509810501
Age: 504
Via: 1.1 varnish
Connection: keep-alive
X-Cache-Svr: p2137050.pubip.peer1.net
X-Cache: HIT
X-Cache-Hits: 425
Run Code Online (Sandbox Code Playgroud)
我知道返回的数据是gzip压缩的,因为这会返回html,如预期的那样:
$ curl "http://example.com" | gunzip
Run Code Online (Sandbox Code Playgroud)
我不想通过gunzip管道输出,因为脚本在其他站点上按原样运行,并且通过gzip管道将破坏该功能.
一切都空了
有任何想法吗?
我想设置curl使用代理服务器.网址由html表单提供,这不是问题.没有代理,它工作正常.我在这个和其他网站上找到了代码,但它们不起作用.任何帮助找到正确的解决方案将非常感激.我觉得波纹管很接近,但我错过了一些东西.谢谢.
我在这里改编的波纹管代码http://www.webmasterworld.com/forum88/10572.htm,但它返回了第12行丢失的T_VARIABLE的错误消息.
<?
$url = '$_POST[1]';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
curl_setopt($ch, CURLOPT_PROXY, '66.96.200.39:80');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'GET');
curl_setopt ($ch, CURLOPT_HEADER, 1)
curl_exec ($ch);
$curl_info = curl_getinfo($ch);
curl_close($ch);
echo '<br />';
print_r($curl_info);
?>
Run Code Online (Sandbox Code Playgroud)
波纹管从卷曲到代理返回没有内容
<?
$proxy = "66.96.200.39:80";
$proxy = explode(':', $proxy);
$url = "$_POST[1]";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy[0]);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy[1]);
curl_setopt($ch, CURLOPT_HEADER, 1);
$exec = curl_exec($ch);
echo curl_error($ch);
print_r(curl_getinfo($ch));
echo …Run Code Online (Sandbox Code Playgroud)