Harinder
Harinder

Reputation: 1257

PHP Curl CURLOPT_POSTFIELDS with encoding

I am trying to pass value in curl ... here is my code

$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.

How can i do that

Thx

EDIT

I am trying to post google url to a proxy site

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

Upvotes: 1

Views: 15472

Answers (2)

Lawrence Cherone
Lawrence Cherone

Reputation: 46620

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

<?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.

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:

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>';

Upvotes: 3

Marc B
Marc B

Reputation: 360782

$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.

Upvotes: 3

Related Questions