【问题标题】:How to curl via php [closed]如何通过 php curl [关闭]
【发布时间】:2021-04-20 09:56:20
【问题描述】:

我必须通过 php 脚本执行以下操作

curl --location --request POST 'https://api.mydomain.in/api/comm/wa/send/text' \
--header 'Content-Type: application/json' \
--header 'token: xyz123456' \
--data-raw '{
   "phone": "8822992929",
   "ID": 26,
   "text": "Dear Customer. Thank you for your purchase. "

}'

如何通过 php curl exec 执行此操作。我看不出如何在这个中传递数据

【问题讨论】:

标签: php curl


【解决方案1】:

您应该尝试从您的角度做某事并在问题中提及它。无论如何,这是您问题的解决方案

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.mydomain.in/api/comm/wa/send/text',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{
   "phone": "8822992929",
   "ID": 26,
   "text": "Dear Customer. Thank you for your purchase. "

}',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
    'token: xyz123456'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

【讨论】:

    【解决方案2】:
    $post = array( "phone" => "8822992929",
    "ID" => 26,
    "text" => "Dear Customer. Thank you for your purchase.");
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    /*$headers = array(
        'Content-Length: 0',
        'Content-Type: application/json; charset=us-ascii',
        'token: xyz123456',
    );*/
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    //curl_setopt($ch, CURLOPT_USERNAME, $username);
    //curl_setopt($ch, CURLOPT_USERPWD, $password);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    $result = curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch);
    

    这是我用于我的 php.ini 的版本。您需要编辑 $headers 数组以使用 json 并发送令牌。很难说,但我认为您需要 data-raw 作为“类似 POST”的数组,然后将其转到 CURLOPT_POSTFIELDS, $post 部分。用户名、密码是可选的,您可能还需要更改身份验证。

    【讨论】:

      猜你喜欢
      • 2013-02-18
      • 2012-09-28
      • 2014-02-11
      • 2011-10-20
      • 2012-11-15
      • 1970-01-01
      • 2013-05-15
      • 1970-01-01
      相关资源
      最近更新 更多