Sunday, January 21, 2018

Send ajax request through CURL

Leave a Comment

The url is

http://milanoweb.milanocloud.com:8080/index.html?store=e672b526-9f65-48f9-b8aa-e6297bce14d8 

Inspecting on network tab, this url http://milanoweb.milanocloud.com:8080/OLB/businessInformation?key=e672b526-9f65-48f9-b8aa-e6297bce14d8&_=1516467005097

sends a ASP.NET Session ID cookie through XHR request (in the network tab) this cookie is used for subsequent requests. I can work to the point of showing services, but for booking this cookie gets invalid (through CURL), but if I copy/paste the cookie from the browser, the booking goes through.

I need to use CURL to get a valid Session ID cookie to make the booking. Can anyone try this? And maybe find a workaround?

Here's what I have tried.

This is my CURL request.

$ch = curl_init(); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64)'); curl_setopt($ch, CURLOPT_REFERER, 'server's url'); curl_setopt($ch, CURLOPT_AUTOREFERER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array(     'Accept:application/json, text/javascript, */*; q=0.01',     'Accept-Encoding:gzip, deflate',     'Accept-Language:en-US,en;q=0.9',     'Connection:keep-alive',     'Content-Type: application/json; charset=utf-8',     'X-Requested-With: XMLHttpRequest',     )); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_COOKIEJAR, base_path().'/cookies.txt'); curl_setopt($ch, CURLOPT_COOKIEFILE, base_path().'/cookies.txt'); $buffer = curl_exec($ch); if(curl_error($ch)) {     $buffer =   curl_error($ch); } curl_close($ch);  return $buffer; 

This curl request works to the point till showing services and getting phone verification code, but for booking the ASP.NET session id cookie stored in the cookies.txt is not valid and the booking does not go through.

But, this ajax request goes through my localhost, but since my live website uses HTTPS I cannot really use it.

$.ajax({     type: "get",     xhrFields: { withCredentials:true },     url: http://apiendpoint.com,     success: function(data)     {       // console.log(data);     } }) 

This ajax request goes through, and the booking can be done.But I need to use CURL.

If you are testing it, please use a name of testing and testing

3 Answers

Answers 1

in chrome, you can copy a working curl expression from developer toolbar. Try with that one from cli. If that works, you can figure out which parts are required and which parts are not. Then you can transcript it to php.

developer toolbar -> network -> select a file -> right click - copy -> copy as curl

If you have doubts if the same thing happens from php than from curl, just try it with requestbin.

Answers 2

I think it is possible the header for token may not be what you think it is, since given $a==1, '$a' converts to $a, but "$a" converts to "1" (notice single quotes vs double quotes).

in your example, try replacing:

'__RequestVerificationToken: $token' 

with:

"__RequestVerificationToken: $token" 

and let us know if that solves the problem.

Consider using passthru("curl command here..."); using the suggestion from lintabá

Answers 3

You can use 2-stage request

1 curl .... -c ${cookie_file}

2 curl .... -b ${cookie_file} -c ${cookie_file}

This should work. first is basicly gets the cookie with session id 2nd do the real request

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment