Deze vraag behoort waarschijnlijk niet tot één enkele ontwikkelaarsgroep, maar de Esri-Leaflet implementatie is het uiteindelijke doel, en heeft waarschijnlijk ontwikkelaars die het meest vertrouwd zijn met de stack - dus hier gaan we:<\/P>
<\/P>
Ik heb een standalone ArcGIS server situatie en wil beveiligde feature services benaderen. Mijn site draait op PHP 7.3 en heeft toegang tot cURL. Idealiter wil ik het token verzamelen met behulp van in PHP opgeslagen referenties, met cURL, en dat token vervolgens aan Esri-Leaflet leveren wanneer ik een addtomap doe, zodat de referenties veilig blijven. <\/P>
De fout waar ik tegenaan loop bij de respons op het verzoek is de nogal generieke:<\/P>
{"error":{"code":498,"message":"Invalid Token","details":[]}}<\/P><\/BLOCKQUOTE> <\/P>Tijdelijk is er hier een url:<\/P>http:\/\/52.65.144.150\/esri-js-demo-page\/<\/A><\/P> <\/P>Dus, wat heb ik tot nu toe gedaan? Laten we een beveiligde esri demo service gebruiken ter illustratie, waarbij ik commentaarsecties heb achtergelaten waar ik ofwel:<\/P>Heb geprobeerd de referer handmatig in te stellen zodat cURL dezelfde referer gebruikt als de referer die vanuit de site wordt doorgegeven (hopelijk). Dit omvatte de pagina die de kaart aanroept, zoals te zien is door te kijken naar de requests referer die in Chrome wordt doorgegeven, en ook door ervoor te zorgen dat de referer die door PHP wordt uitgegeven overeenkomt met die welke Chrome verzendt. Over het algemeen wil ik deze aanpak niet gebruiken omdat ik meerdere pagina's deze PHP-code wil laten gebruiken.<\/LI>Heb geprobeerd in plaats daarvan het IP-adres van de bezoeker te gebruiken. Dit genereerde een token maar resulteerde in hetzelfde probleem met een ongeldig token. Mijn organisatie kan ook verzoeken via verschillende eindpunten sturen, dus dit is een mogelijke instabiliteit.<\/LI>Heb geprobeerd de referer in te stellen met andere PHP-methoden zoals $_SERVER['HTTP_REFERER'];<\/LI>Je kunt ook mijn regex zien voor het parsen van de tokens - dit verwijdert gewoon de rest van de respons zodat alleen de token string overblijft. {".,}. Ik stuur dus geen vervaldatum terug naar de server tijdens het laagverzoek - ik zie nergens in documentatie dat dit vereist is.<\/LI><\/UL> <\/P> <\/P> <?php
{"error":{"code":498,"message":"Invalid Token","details":[]}}<\/P><\/BLOCKQUOTE>
Tijdelijk is er hier een url:<\/P>
http:\/\/52.65.144.150\/esri-js-demo-page\/<\/A><\/P> <\/P>Dus, wat heb ik tot nu toe gedaan? Laten we een beveiligde esri demo service gebruiken ter illustratie, waarbij ik commentaarsecties heb achtergelaten waar ik ofwel:<\/P>Heb geprobeerd de referer handmatig in te stellen zodat cURL dezelfde referer gebruikt als de referer die vanuit de site wordt doorgegeven (hopelijk). Dit omvatte de pagina die de kaart aanroept, zoals te zien is door te kijken naar de requests referer die in Chrome wordt doorgegeven, en ook door ervoor te zorgen dat de referer die door PHP wordt uitgegeven overeenkomt met die welke Chrome verzendt. Over het algemeen wil ik deze aanpak niet gebruiken omdat ik meerdere pagina's deze PHP-code wil laten gebruiken.<\/LI>Heb geprobeerd in plaats daarvan het IP-adres van de bezoeker te gebruiken. Dit genereerde een token maar resulteerde in hetzelfde probleem met een ongeldig token. Mijn organisatie kan ook verzoeken via verschillende eindpunten sturen, dus dit is een mogelijke instabiliteit.<\/LI>Heb geprobeerd de referer in te stellen met andere PHP-methoden zoals $_SERVER['HTTP_REFERER'];<\/LI>Je kunt ook mijn regex zien voor het parsen van de tokens - dit verwijdert gewoon de rest van de respons zodat alleen de token string overblijft. {".,}. Ik stuur dus geen vervaldatum terug naar de server tijdens het laagverzoek - ik zie nergens in documentatie dat dit vereist is.<\/LI><\/UL> <\/P> <\/P> <?php
Dus, wat heb ik tot nu toe gedaan? Laten we een beveiligde esri demo service gebruiken ter illustratie, waarbij ik commentaarsecties heb achtergelaten waar ik ofwel:<\/P>
<?php
Fixed by treating the json object as json properly.
<SPAN class="delimiter token"><?php</SPAN> <SPAN class="comment token">//Establish our requesting url</SPAN> <SPAN class="keyword token">global</SPAN> <SPAN class="variable token">$wp</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="variable token">$RequestingPage</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="token function">home_url</SPAN><SPAN class="punctuation token">(</SPAN> <SPAN class="variable token">$wp</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">></SPAN><SPAN class="property token">request</SPAN> <SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">//Check to see if the request came from the proper application</SPAN> <SPAN class="comment token">//The url to the token endpoint of the server containing the secure service</SPAN> <SPAN class="variable token">$url</SPAN> <SPAN class="operator token">=</SPAN> "https<SPAN class="punctuation token">:</SPAN><SPAN class="comment token">//someplace.com/arcgis/tokens/";</SPAN> <SPAN class="comment token">//The fields included in the request</SPAN> <SPAN class="variable token">$fields</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">array</SPAN><SPAN class="punctuation token">(</SPAN> <SPAN class="string token">'request'</SPAN> <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="string token">'getToken'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'username'</SPAN> <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="string token">'username1'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'password'</SPAN> <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="string token">'password1'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'expiration'</SPAN> <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="number token">120</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'clientid'</SPAN> <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="string token">'ref.'</SPAN> <SPAN class="punctuation token">.</SPAN> <SPAN class="variable token">$RequestingPage</SPAN> <SPAN class="punctuation token">.</SPAN> <SPAN class="string token">'/'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'f'</SPAN> <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="string token">'json'</SPAN> <SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="variable token">$fields_string</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">foreach</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$fields</SPAN> <SPAN class="keyword token">as</SPAN> <SPAN class="variable token">$key</SPAN><SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN><SPAN class="variable token">$value</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="variable token">$fields_string</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="operator token">=</SPAN> <SPAN class="variable token">$key</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="string token">'='</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="variable token">$value</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="string token">'&'</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="comment token">//Instantiate Curl</SPAN> <SPAN class="variable token">$ch</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="token function">curl_init</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$url</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="token function">curl_setopt</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$ch</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="token constant">CURLOPT_SSL_VERIFYPEER</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="token boolean">false</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="token function">curl_setopt</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$ch</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="token constant">CURLOPT_POST</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="token function">count</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$fields</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="token function">curl_setopt</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$ch</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="token constant">CURLOPT_POSTFIELDS</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="variable token">$fields_string</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="token function">curl_setopt</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$ch</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="token constant">CURLOPT_RETURNTRANSFER</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="token boolean">true</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="token function">curl_setopt</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$ch</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="token constant">CURLOPT_REFERER</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="variable token">$RequestingPage</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="variable token">$result</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="token function">curl_exec</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$ch</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">//If Curl was not instantiated successfully</SPAN> <SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="operator token">!</SPAN><SPAN class="token function">curl_exec</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$ch</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="keyword token">echo</SPAN> <SPAN class="string token">'An error has occurred: '</SPAN> <SPAN class="punctuation token">.</SPAN> <SPAN class="token function">curl_error</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$ch</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="comment token">//Otherwise return the result</SPAN> <SPAN class="keyword token">else</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="comment token">//Treat the response as JSON, and split by attribute.</SPAN> <SPAN class="variable token">$someObject</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="token function">json_decode</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$result</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">echo</SPAN> '<SPAN class="markup token"><SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"><</SPAN>script</SPAN> <SPAN class="attr-name token">type</SPAN><SPAN class="attr-value token"><SPAN class="punctuation token">=</SPAN><SPAN class="punctuation token">"</SPAN>text/javascript<SPAN class="punctuation token">"</SPAN></SPAN><SPAN class="punctuation token">></SPAN></SPAN></SPAN>'<SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">echo</SPAN> <SPAN class="string token">"\r\n"</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">echo</SPAN> <SPAN class="string token">'var val = "'</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="variable token">$someObject</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">></SPAN><SPAN class="property token">token</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="string token">'"'</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">echo</SPAN> <SPAN class="string token">"\r\n"</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">echo</SPAN> '<SPAN class="markup token"><SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"></</SPAN>script</SPAN><SPAN class="punctuation token">></SPAN></SPAN></SPAN>'<SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">echo</SPAN> <SPAN class="string token">"\r\n"</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="comment token">//Close Curl</SPAN> <SPAN class="token function">curl_close</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$ch</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="token function">add_action</SPAN><SPAN class="punctuation token">(</SPAN> <SPAN class="string token">'wp_head'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'ple_hook_javascript'</SPAN> <SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
This works out for me. Thanks Simon
Glad it helped!
I guess what I didn't mention in my initial series of posts was that the script returns the variable "var" which contains the token. When collecting a layer with ESRI-Leaflet you can supply this token as part of the URL. Just for anyone else who happens upon this in the future.
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.