{"id":8065,"date":"2015-11-23T18:40:05","date_gmt":"2015-11-23T18:40:05","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/23\/how-to-identify-the-results-of-guzzle-multiple-requests-level-very-hard-open-source-projects-guzzle-guzzle\/"},"modified":"2022-08-30T15:03:02","modified_gmt":"2022-08-30T15:03:02","slug":"how-to-identify-the-results-of-guzzle-multiple-requests-level-very-hard-open-source-projects-guzzle-guzzle","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/23\/how-to-identify-the-results-of-guzzle-multiple-requests-level-very-hard-open-source-projects-guzzle-guzzle\/","title":{"rendered":"How to identify the results of Guzzle multiple requests? (level: very hard)-open source projects guzzle\/guzzle"},"content":{"rendered":"<p><strong>A. Summary<\/strong><\/p>\n<p>As its title, Guzzle allows to send multiple requests at once to save time, as in documentation.<\/p>\n<pre><code>$responses = $client-&gt;send(array(\n    $requestObj1,\n    $requestObj2,\n    ...\n));\n\n(given that each request object is an instance of\nGuzzle\\Http\\Message\\EntityEnclosingRequestInterface)\n<\/code><\/pre>\n<p>When responses come back, to identify which response is for which request, we can loop through each request and get the response (only available after executing the above command):<\/p>\n<pre><code>$response1 = $requestObj1-&gt;getResponse();\n$response2 = $requestObj2-&gt;getResponse();\n...\n<\/code><\/pre>\n<p><strong>B. Problem<\/strong><\/p>\n<p>If the request object contains the same data. It&#8217;s impossible to identify the original request.<\/p>\n<p>Assume we have the following scenario where we need to create 2 articles: A and B on a distance server: something.com\/articles\/create.json<\/p>\n<p>Each request has same POST data:<\/p>\n<pre><code>subject: This is a test article\n<\/code><\/pre>\n<p>After created, the Guzzle responses with 2 location come back:<\/p>\n<pre><code>something.com\/articles\/223.json\nsomething.com\/articles\/245.json\n<\/code><\/pre>\n<p>Using the above method to link response-to-its-request, we still don&#8217;t know which response is for which article, <strong>because the request object is exactly the same<\/strong>.<\/p>\n<p>Hence in my database I cannot write down the result:<\/p>\n<pre><code>article A -&gt; Location: 245.json\narticle B -&gt; Location: 223.json\n<\/code><\/pre>\n<p>because it can be the other way arround:<\/p>\n<pre><code>article A -&gt; Location: 223.json\narticle B -&gt; Location: 245.json\n<\/code><\/pre>\n<p>A solution is to put some extra parameter in the POST request, e.g.<\/p>\n<pre><code>subject: This is a test article\nrecord: A\n<\/code><\/pre>\n<p>However, the distance server will return error and does not create article because it does not understand the key &#8220;record&#8221;. The distance server is a third party server and I cannot change the way it works.<\/p>\n<p>Another proper solution for this is to set some specific id\/tag on the request object, so we can identify it afterwards. However, I&#8217;ve looked through the documentation but there is no method to uniquely identity the request like<\/p>\n<pre><code>$request-&gt;setID(\"id1\")\n\nor\n\n$request-&gt;setTag(\"id1\")\n<\/code><\/pre>\n<p>This has been bugging me for months and still cannot resolve this issue.<\/p>\n<p>If you have solution, please let me know.<\/p>\n<p>Many many thanks and you&#8217;ve saved me!!!!<\/p>\n<p>Thanks for reading this long post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A. Summary As its title, Guzzle allows to send multiple requests at once to save time, as in documentation. $responses = $client-&gt;send(array( $requestObj1, $requestObj2, &#8230; )); (given that each request object is an instance of Guzzle\\Http\\Message\\EntityEnclosingRequestInterface) When responses come back, to identify which response is for which request, we can loop through each request and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8065","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8065","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=8065"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8065\/revisions"}],"predecessor-version":[{"id":8653,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8065\/revisions\/8653"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=8065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=8065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=8065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}