我如何从Goutte获取响应状态代码

时间:2019-09-08 21:28:41

标签: php web-crawler goutte

我正在使用Goutte来解析某些网站。

这是我的代码示例

$goutteClient = new Client();
$response = $goutteClient->setHeader('User-Agent', $userAgent)
->setClient($guzzleClient)
->request('GET', 'https://example.com');

$responseSymfony\Component\DomCrawler\Crawler

的实例

那么,如何获取响应状态代码以检查它是404错误还是其他错误?

3 个答案:

答案 0 :(得分:1)

对我有帮助

if ($goutteClient->getResponse()->getStatusCode() === 404){}

答案 1 :(得分:0)

$goutteClient->getResponse()将为您提供最后一个请求的响应对象。

完整代码是:

$res = $goutteClient->getResponse();
if ($res->getStatusCode() === 404) {}

答案 2 :(得分:0)

 handleSubmitChat = (event) => {
            //algorithm chatbot algorithm
            event.preventDefault();

            //for single string match example: user can input water an get water word related answers...
            // let matches = this.state.keywords.filter(s => s.includes(this.state.humanarea))
            // Split spaces after user sends
            let searchString = this.state.humanarea.toLowerCase()
            let searchStringSplit = searchString.split(/(\s+)/).filter(function (e) { return e.trim().length >= 3; })
            if (searchStringSplit.length >= 1) {
                this.state.matches = this.state.keywords.filter(replykey => {
                    let containsAtLeastOneWord = false;
                    // If at least a word is matched it returns the matching answers!
                    searchStringSplit.forEach(word => {
                        if (replykey.toLowerCase().includes(word)) {
                            containsAtLeastOneWord = true;
                        }
                    })
                    if (containsAtLeastOneWord) {
                        return replykey

                    }console.log(replykey)

                })
                this.setState({
                    botarea: this.state.matches
                })
                console.log(this.state.matches)

            }
        }



 constructor(props) {
            super(props);

            this.state = {
                humanarea: '',
                matches: [''],
                keywords: [
                    'hi',
                    'hey i am fine',
                    'hey this is flower chatbot',
                    'i need water',
                    'water makes me bloom',
                    'protect me i am hurt',
                    'disable my defence so bees can come',
                    'bees can make honey all day',
                    'thanks bees for making honey ',
                    'you are awesome',
                    'only you can touch me'
                ],

                dictonary: [ //synonyms
                    {
                        greet: `//user input should first match these key words as synonyms...`
                            [
                                'greetings',
                                'hi',
                                'hey',
                                'howdy',
                                'welcome',
                                'good morning',
                                'how are you',
                                'how goes it',
                                'howdy-do',
                                'whats happening',
                                'whats up'
                            ],
`//other synonyms....`
                        group2: ['water', 'need',],
                        group3: ['bloom',],
                        group4: ['bees',],
                        group5: ['honey',]
                    }

                ]

            }