如何使用PHP在cURL POST请求中使用数组

时间:2018-09-07 08:36:30

标签: php curl

我想知道如何使此代码支持数组?

我正在尝试通过股票筛选器中的php curl发送参数以在此页面中显示结果: https://finance.yahoo.com/screener/unsaved/f0171a68-053e-42f2-a941-d6ecdf2ba6d1?offset=25&count=25 parameters 这是我的PHP代码

loadHTML

3 个答案:

答案 0 :(得分:0)

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parameters));

更改此行。如果您的问题是可以通过curl发送正文,则可能需要将其作为json发送,因此请尝试

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($parameters,true));

我看到您的评论,您已尝试将其作为数组发送,并且我认为它不起作用,因此我几乎可以确定在您的帖子中您需要发送一个json(这是帖子请求的最常见主体格式)

答案 1 :(得分:0)

工作示例

$data=array();
$data['amount']=100;
$data['to']='test';
$json=json_encode($data);
$header = array('Content-Type: application/json');
$handle = curl_init($url);
    curl_setopt($handle, CURLOPT_POST, true);
    curl_setopt($handle,CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($handle, CURLOPT_POSTFIELDS, $json);
    curl_setopt($handle, CURLOPT_HTTPHEADER, $header);
$response=curl_exec($handle);

答案 2 :(得分:0)

谢谢pr1nc3和Gurpal singh

我更改了这一行

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parameters));

为此

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($parameters,true));

但是我总是得到这个结果

'{"error":{"result":null,"error":{"code":"internal-error","description":"STREAMED"}}}'

再次输入代码

<?php

    $url = 'https://query1.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'
  ];

  $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, json_encode($parameters,true));
  // curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parameters));
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

  $response = curl_exec($ch);
  curl_close($ch);

  var_dump($response);

?>

我认为错误出在变量$ parameters中,但我看不到哪里

当我比较视图源发送时

{"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"}

随着我的变量$ parameters的存在差异

{"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"}