简体   繁体   English

带有编码的PHP Curl CURLOPT_POSTFIELDS

[英]PHP Curl CURLOPT_POSTFIELDS with encoding

I am trying to pass value in curl ... here is my code 我正在尝试传递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);

Problem is CURL is taking 'this&q=love' as 2 values ... I cannot encode this value (http_build_query or urlecode), i have to pass this value with '&' in it. 问题是CURL将'this&q = love'作为2个值...我无法对该值进行编码(http_build_query或urlecode),我必须在其中传递带有'&'的值。

How can i do that 我怎样才能做到这一点

Thx 谢谢

EDIT 编辑

I am trying to post google url to a proxy site 我正在尝试将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);

Here is the full code 这是完整的代码

$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);

If you pass an array to curl, it'll basically do this exact sequence itself anyways. 如果传递数组进行卷曲,则基本上无论如何它都会自行执行此精确序列。

If you use http_build_query() it will encode the values as expected, also google is expecting GET not POST, A simple test proves it 如果您使用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>';

Also you still need to use http:// , if you are ever expecting https:// schema then you should also add the following options to the curl request. 同样,您仍然需要使用http:// ,如果您期望使用https://模式,那么还应该在curl请求中添加以下选项。

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

good luck 祝好运

2nd Edit 第二次编辑

This seems to work, basically the u is the param the url following is the string, thats why http_build_query failed, my-bad i got abit confused with the double proxy idea ;p: 这似乎可行,基本上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.

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