I am wondering how do I make this code support arrays? "jp"]}]}] {size: 25, offset: 25, sortField: "intradaymarketcap", sortType: "DESC", quoteType: "EQUITY",…} offset:25 query:{operator: "AND",…} operands:[{operator: "or", operands: [{operator: "EQ", operands: ["region", 0:{operator: "or", operands: [{operator: "EQ", operands: ["region", "jp"]}]} operands:[{operator: "EQ", operands: ["region", "jp"]}] 0:{operator: "EQ", operands: ["region", "jp"]} operands:["region", "jp"] 0:"region" 1:"jp" operator:"EQ" operator:"or" operator:"AND" quoteType:"EQUITY" size:25 sortField:"intradaymarketcap" sortType:"DESC" topOperator:"AND" userId:"HFEELK3VBE3KPE4MGEA6PZTXXL" userIdType:"guid" so what i do $url = 'http://api.example.com/api'; $parameters = [ 'size' => 25, 'offset' => 50, 'sortField' => 'intradaymarketcap', 'sortType' => 'DESC', 'quoteType' => 'EQUITY', 'topOperator' => 'AND', 'query' => array( 'operator' => 'AND', 'operands'=> array( 'operator' => 'or', 'operands' => array( 'operator' => 'EQ', 'operands' => array("region","jp") ) ) ), 'userId' => 'HFEELK3VBE3KPE4MGEA6PZTXXL', 'userIdType' => 'guid' ]; $headers = [ 'Accept: application/json, text/javascript, */*; q=0.01', 'Accept-Language: en-US,en;q=0.5', 'X-Requested-With: XMLHttpRequest', 'Connection: keep-alive', 'Pragma: no-cache', 'Cache-Control: no-cache', ]; $cookie = tmpfile(); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.31'); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parameters)); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($ch); curl_close($ch); print_r($response); PHP: is it correct? thank you Nuno
Thank you Sarahk Convert to json string is it to use json_encode()? $myparameters= json_encode($parameters);
It would be if you were using php but when they fill in the form you need javascript. Try something like this // Find our form in the DOM using its class name. var form = document.getElementById('myform'); // Get the form data with our (yet to be defined) function. var data = getFormDataAsJSON(form); Code (markup):
I just re-read your first post - are you sending info from your server to another website? If so, then yes, you can use PHP. There are good examples online - here's a basic one $url = 'https://siteiwanttosenddatato.com/contact/'; $data = array("name" => "Sarah", "country" => "NZ"); $data_string = json_encode($data); $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string)) ); $result = curl_exec($ch); PHP:
i adapted your example to my script ... but i have this error {"finance":{"result":null,"error":{"code":"Bad Request","description":"com.google.gson.JsonObject cannot be cast to com.google.gson.JsonArray"}}}' (length=145) Code (markup): here is the code i'am trying to send parameters via php curl in a stock screener to have the result in this page: https://finance.yahoo.com/screener/unsaved/f0171a68-053e-42f2-a941-d6ecdf2ba6d1?offset=25&count=25 here is my php code $url = 'https://query2.finance.yahoo.com/v1/finance/screener?lang=en-US®ion=US&formatted=true&corsDomain=finance.yahoo.com'; // $url = 'https://finance.yahoo.com/screener/unsaved/f0171a68-053e-42f2-a941-d6ecdf2ba6d1'; $parameters = [ 'size' => 25, 'offset' => 50, 'sortField' => 'intradaymarketcap', 'sortType' => 'DESC', 'quoteType' => 'EQUITY', 'topOperator' => 'AND', 'query' => array( 'operator' => 'AND', 'operands'=> array( 'operator' => 'or', 'operands' => array( 'operator' => 'EQ', 'operands' => array("region","jp") ) ) ), 'userId' => 'HFEELK3VBE3KPE4MGEA6PZTXXL', 'userIdType' => 'guid' ]; $data_string = json_encode($parameters); $headers = [ 'Accept: application/json, text/javascript, */*; q=0.01', 'Accept-Language: en-US,en;q=0.5', 'X-Requested-With: XMLHttpRequest', 'Connection: keep-alive', 'Pragma: no-cache', 'Cache-Control: no-cache', ]; $cookie = tmpfile(); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.31'); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string)) ); $response = curl_exec($ch); curl_close($ch); var_dump($response); PHP:
There's a lot going on on that page. Do you need to automate the running of it? I'd probably run an ajax hijacker over the page until I worked out exactly which call is getting the data. I haven't used curl on ajax forms so I'm no good there. You've checked to see if they have an api, right?
i think the error is in the variable $parameters but i don't see where when i compare view source send {"size":25,"offset":0,"sortField":"intradaymarketcap","sortType":"DESC","quoteType":"EQUITY","topOperator":"AND","query":{"operator":"AND","operands":[{"operator":"or","operands":[{"operator":"EQ","operands":["region","jp"]}]}]},"userId":"HFEELK3VBE3KPE4MGEA6PZTXXL","userIdType":"guid"} PHP: with my variable $parameters, there are diferences {"size":25,"offset":50,"sortField":"intradaymarketcap","sortType":"DESC","quoteType":"EQUITY","topOperator":"AND","query":{"operator":"AND","operands":{"operator":"or","operands":{"operator":"EQ","operands":["region","jp"]}}},"userId":"HFEELK3VBE3KPE4MGEA6PZTXXL","userIdType":"guid"} PHP: i always have this result '{"error":{"result":null,"error":{"code":"internal-error","description":"STREAMED"}}}' Code (markup):
I have some ideas for you: 1. Try to execute a success request through jQuery. 2. Try sending an empty request without a $parameters variable via php and examine the error message. 3. The "internal-error" erros sounds like a wrong json object format. Try to remove a part of parametrs.
Serialize the array before passing and unserialize it upon receiving, http://php.net/manual/en/function.serialize.php