我试图在卷曲中传递价值......这是我的代码
$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),我必须使用'&'传递此值在它。
我该怎么做
THX
修改
我正在尝试将google网址发布到代理网站
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);
这是完整的代码
答案 0 :(得分:3)
$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);
如果你将一个数组传递给curl,那么它基本上就会完成这个确切的序列。
答案 1 :(得分:2)
如果你使用http_build_query()它会按预期编码值,谷歌也期望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_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
祝你好运
这似乎有效,基本上u
是后面的url是字符串的param,这就是为什么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>';