Back to articles
Pass Python Requests to PHP via PSR-7 Message Format
NewsTools

Pass Python Requests to PHP via PSR-7 Message Format

via Dev.to TutorialRecca Tsai

Originally published at recca0120.github.io Sometimes Python already has a great package for scraping web data, but all the downstream processing is in PHP. The question is: how do you pass a Python requests response back to PHP intact? composer require guzzlehttp/psr7 symfony/process Why PSR-7 Message Format HTTP messages are a plain-text protocol, and PSR-7 defines a standard message interface. As long as the Python side outputs the response as an HTTP message string, PHP can parse it directly into a ResponseInterface using GuzzleHttp\Psr7 's Message::parseResponse — no manual header/body splitting needed. Python Side: Convert requests Response to PSR-7 Message import argparse from urllib.parse import urlparse import requests from requests import Response , PreparedRequest parser = argparse . ArgumentParser () parser . add_argument ( " url " ) args = parser . parse_args () def has_header ( headers , key : str , value : str = None ): for ( k , v ) in headers : if k . lower () == key .

Continue reading on Dev.to Tutorial

Opens in a new tab

Read Full Article
3 views

Related Articles