[英]PHP Curl CURLOPT_POSTFIELDS with encoding
我正在尝试传递curl中的值...这是我的代码
$POSTVARS = array('u'=>'this&q=love', 'v' => 'hate');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST ,1);
curl_setopt($ch, CURLOPT_POSTFIELDS ,$POSTVARS);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($ch, CURLOPT_HEADER ,0); // DO NOT RETURN HTTP HEADERS
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1); // RETURN THE CONTENTS OF THE CALL
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile.txt");
$Rec_Data = curl_exec($ch);
curl_close($ch);
问题是CURL将'this&q = love'作为2个值...我无法对该值进行编码(http_build_query或urlecode),我必须在其中传递带有'&'的值。
我怎样才能做到这一点
谢谢
编辑
我正在尝试将google url发布到代理站点
function proxy_browse($url,$POSTVARS){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST ,1);
curl_setopt($ch, CURLOPT_POSTFIELDS ,$POSTVARS);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($ch, CURLOPT_HEADER ,0); // DO NOT RETURN HTTP HEADERS
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1); // RETURN THE CONTENTS OF THE CALL
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile.txt");
$Rec_Data = curl_exec($ch);
curl_close($ch);
return $Rec_Data;
}
$keyword = 'u=google.com/complete/search?output=toolbar&q=love';
$url = 'http://hostfast.info/includes/process.php?action=update';
$proxy_browse = proxy_browse($url, $keyword);
这是完整的代码
$arr = array('u' => 'this&q=love', 'v' => 'hate');
$query = http_build_query($arr); // produces: u=this%26q%3Dlove&v=hate
curl_setopt($ch, CURLOPT_POSTFIELDS , $query);
如果传递数组进行卷曲,则基本上无论如何它都会自行执行此精确序列。
如果您使用http_build_query() ,它将按预期对值进行编码,Google也期望GET而不是POST,一个简单的测试证明了这一点
<?php
function proxy_browse($url, $POSTVARS = array()){
$ch = curl_init($url);
if(!empty($POSTVARS)){
curl_setopt($ch, CURLOPT_POST ,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($POSTVARS));
}
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_HEADER,0); // DO NOT RETURN HTTP HEADERS
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // RETURN THE CONTENTS OF THE CALL
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile.txt");
$Rec_Data = curl_exec($ch);
curl_close($ch);
return $Rec_Data;
}
//Wrong way, google expects a GET request
$url = 'http://google.com/complete/search';
$proxy_browse = proxy_browse($url,array('output'=>'toolbar','q'=>'love'));
//Error 405 (Method Not Allowed)!!
echo '<pre>'.htmlentities($proxy_browse).'</pre>';
//Working way
$url = 'http://google.com/complete/search?output=toolbar&q=love';
$proxy_browse = proxy_browse($url);
/*
<?xml version="1.0"?><toplevel><CompleteSuggestion><suggestion data="love quotes"/></Com ...\snip*/
echo '<pre>'.htmlentities($proxy_browse).'</pre>';
同样,您仍然需要使用http://
,如果您期望使用https://
模式,那么还应该在curl请求中添加以下选项。
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
祝好运
这似乎可行,基本上u
是参数,URL之后是字符串,这就是为什么http_build_query失败的原因,我不好,我对双重代理的想法有些困惑; p:
function proxy_browse($url, $POSTVARS = array()){
$ch = curl_init($url);
if(!empty($POSTVARS)){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($POSTVARS));
}
curl_setopt($ch, CURLOPT_FOLLOWLOCATION , 1);
curl_setopt($ch, CURLOPT_HEADER, 0); // DO NOT RETURN HTTP HEADERS
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // RETURN THE CONTENTS OF THE CALL
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile.txt");
$Rec_Data = curl_exec($ch);
curl_close($ch);
return $Rec_Data;
}
$keyword = array('u'=>'google.com/complete/search?output=toolbar&q=love');
$url = 'http://hostfast.info/includes/process.php?action=update';
$proxy_browse = proxy_browse($url, $keyword);
echo '<pre>'.htmlentities($proxy_browse).'</pre>';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.