FriendsOfPHP / Goutte

Goutte, a simple PHP Web Scraper
MIT License
9.26k stars 1.01k forks source link

Is there any method to get the final http request code? #294

Closed TonyGao closed 7 years ago

TonyGao commented 7 years ago

Hi, there

I'm trying to use goutte to make a post request for a url, and it's source code like below

$this->client->request(
            'POST',
            $postUrl,
            [
                'headers' => [
                    'Content-Type' => 'application/x-www-form-urlencoded',
                    'User-Agent' => ApiURL::$USER_AGENT,
                    'Referer' => ApiURL::$url('referer'),
                    'Origin' => ApiURL::getOrigin(),
                ],
                'form_params' => [
                    'r' => json_encode($jsonObject)
                ]
            ],
            array(),
            [
                'HTTP_USER_AGENT' => ApiURL::$USER_AGENT,
                'HTTP_Referer' => ApiURL::$url('referer'),
                'HTTP_Origin'  => ApiURL::getOrigin()
            ]
        );

And I dump it like this

dump($this->client->getInternalRequest());

After that, I got the result in console, like below object

Symfony\Component\BrowserKit\Request {#428
  #uri: "http://d1.web2.qq.com/channel/login2"
  #method: "POST"
  #parameters: array:2 [
    "headers" => array:4 [
      "Content-Type" => "application/x-www-form-urlencoded"
      "User-Agent" => "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"
      "Referer" => "http://d1.web2.qq.com/proxy.html?v=20151105001&callback=1&id=2"
      "Origin" => "http://d1.web2.qq.com"
    ]
    "form_params" => array:1 [
      "r" => "{"ptwebqq":"d9503b527e049bae876ee38c43f540617921b3c3cd8830154868631a0492577c","clientid":53999199,"psessionid":"","status":"online"}"
    ]
  ]
  #files: []
  #cookies: array:9 [
    "pt2gguin" => "o0363719938"
    "uin" => "o0363719938"
    "skey" => "@v3FaMRcL5"
    "ptisp" => "cnc"
    "RK" => "9UnP0xf6Xs"
    "ptwebqq" => "d9503b527e049bae876ee38c43f540617921b3c3cd8830154868631a0492577c"
    "p_uin" => "o0363719938"
    "p_skey" => "3efwutpDa27Mz8-C69iQn155RHeAcPhmxV7pvFmhwvo_"
    "pt4_token" => "EbPOGduMntTt6Fkne8i6WJVORC*cTVziVLDFlObetg4_"
  ]
  #server: array:6 [
    "HTTP_USER_AGENT" => "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"
    "HTTP_Referer" => "http://d1.web2.qq.com/proxy.html?v=20151105001&callback=1&id=2"
    "HTTP_Origin" => "http://d1.web2.qq.com"
    "HTTP_REFERER" => "http://s.web2.qq.com/api/getvfwebqq?ptwebqq=d9503b527e049bae876ee38c43f540617921b3c3cd8830154868631a0492577c&clientid=53999199&psessionid=&t=0.1"
    "HTTP_HOST" => "d1.web2.qq.com"
    "HTTPS" => false
  ]
  #content: null
}

But the response of request is not expected results. However, when I copy and paste the same dumped header value and form data to the postman, I got right response, I don't know why.

image image

The problem is I know something wrong with parameters, but I can't see what going on just like I do in Chrome Developer Tools Network to see the header, form data, etc. Any idea?

TonyGao commented 7 years ago

My bad, I have confused guzzle and browserkit usage, work fine now, close issue.