php curl sending vars using GET wierd results

2019-07-14 14:01发布

问题:

i'm trying to call a url in a page on a remote site. Decided on using curl. On the remote site the url vars are showing as:

$_REQUEST   Array
(
    [var1] => val1
    [amp;var2] => val2
    [amp;var3] => val3
)

the url being called is:

http://site.com/controller/action.php?var1=val1&var2=val2&var3=val3

Notice that I'm not using & in the url, but the request global has it, or nearly - it has amp; instead of & and not & like I used!!! Not that it should have any of them.

The curl object has logged in to a form, set cookies, posted a form on another page, and now this is the last link I have to follow.

here is the php i'm using:

    curl_setopt($this->ch, CURLOPT_URL, "http://site.com/controller/action.php?var1=val1&var2=val2&var3=val3");
    curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($this->ch, CURLOPT_REFERER, "http://site.com/");
    curl_setopt($this->ch, CURLOPT_VERBOSE, 1);
    curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->cookie);
    curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->cookie);
    curl_setopt($this->ch, CURLOPT_POST, 0);
    curl_setopt($this->ch, CURLOPT_HTTPGET, 1);
    $output = curl_exec($this->ch);
    $info = curl_getinfo($this->ch);

I've ran another curl request after this and I'm still logged in so issue is not cookies. Because the last request (the login form) used $_POST have have set the CURLOPT_POST to 0 and the CURLOPT_HTTPGET to 1. Here is the output from $info:

info    Array
(
    [url] => http://site.com/controller/action.php?var1=val1&var2=val2&var3=val3
    [content_type] => text/html; charset=utf-8
    [http_code] => 403
    [header_size] => 403
    [request_size] => 541
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.781186
    [namelookup_time] => 3.7E-5
    [connect_time] => 3.7E-5
    [pretransfer_time] => 4.2E-5
    [size_upload] => 1093
    [size_download] => 3264
    [speed_download] => 4178
    [speed_upload] => 1399
    [download_content_length] => 3264
    [upload_content_length] => 0
    [starttransfer_time] => 0.781078
    [redirect_time] => 0
    [certinfo] => Array
        (
        )

)

if i copy and past $info['url'] into the browser it works. Completely lost, hours lost on the clock, any help would be appreciated ;)

回答1:

Try modifying your code as follows:

$data = array('val1'=>"val1", 'val2'=>"val2", 'val3'=>"val3");
curl_setopt($this->ch, CURLOPT_URL, "http://site.com/controller/action.php");
curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($this->ch, CURLOPT_REFERER, "http://site.com/");
curl_setopt($this->ch, CURLOPT_VERBOSE, 1);
curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->cookie);
curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->cookie);
curl_setopt($this->ch, CURLOPT_POST, 0);
curl_setopt($this->ch, CURLOPT_HTTPGET, 1);
$output = curl_exec($this->ch);
$info = curl_getinfo($this->ch);

EDIT

Removed custom encode function as per Brad's comment - you don't need it, as PHP has a build-in function that does the same thing.



标签: php curl get