1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How do I use arrays in cURL POST requests

Discussion in 'PHP' started by nunonline, Sep 4, 2018.

  1. #1
    I am wondering how do I make this code support arrays?

        1. "jp"]}]}]
          1. {size: 25, offset: 25, sortField: "intradaymarketcap", sortType: "DESC", quoteType: "EQUITY",…}
            1. offset:25
            2. query:{operator: "AND",…}
              1. operands:[{operator: "or", operands: [{operator: "EQ", operands: ["region",
          2. 0:{operator: "or", operands: [{operator: "EQ", operands: ["region", "jp"]}]}
            1. operands:[{operator: "EQ", operands: ["region", "jp"]}]
              1. 0:{operator: "EQ", operands: ["region", "jp"]}
                1. operands:["region", "jp"]
                  1. 0:"region"
                  2. 1:"jp"
                2. operator:"EQ"
            2. operator:"or"
        2. operator:"AND"
      1. quoteType:"EQUITY"
      2. size:25
      3. sortField:"intradaymarketcap"
      4. sortType:"DESC"
      5. topOperator:"AND"
      6. userId:"HFEELK3VBE3KPE4MGEA6PZTXXL"
      7. 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
     
    nunonline, Sep 4, 2018 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,498
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #2
    turn it into a json string.
     
    sarahk, Sep 4, 2018 IP
  3. nunonline

    nunonline Greenhorn

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #4
    Thank you Sarahk
    Convert to json string is it to use json_encode()?
    $myparameters= json_encode($parameters);
     
    nunonline, Sep 4, 2018 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,498
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #4
    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):
     
    sarahk, Sep 4, 2018 IP
  5. nunonline

    nunonline Greenhorn

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #5
    Is it possible to use php to do this?
    I am just begin to learn JavaScript
     
    nunonline, Sep 5, 2018 IP
  6. sarahk

    sarahk iTamer Staff

    Messages:
    28,498
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #6
    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:
     
    sarahk, Sep 5, 2018 IP
  7. nunonline

    nunonline Greenhorn

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #7
    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&region=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:
     
    nunonline, Sep 5, 2018 IP
  8. sarahk

    sarahk iTamer Staff

    Messages:
    28,498
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #8
    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?
     
    sarahk, Sep 5, 2018 IP
  9. nunonline

    nunonline Greenhorn

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #9
    I don’t see any API
    I would like to have the stocks list in that page
     
    nunonline, Sep 6, 2018 IP
  10. nunonline

    nunonline Greenhorn

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #10
    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):
     
    nunonline, Sep 7, 2018 IP
  11. SaleSmith

    SaleSmith Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #11
    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.
     
    SaleSmith, Oct 4, 2018 IP
  12. writeable

    writeable Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #12
    Serialize the array before passing and unserialize it upon receiving, http://php.net/manual/en/function.serialize.php
     
    writeable, Oct 29, 2018 IP