【问题标题】:cURL Request - how do I do it?cURL 请求 - 我该怎么做?
【发布时间】:2023-03-23 16:43:01
【问题描述】:

有人可以指出我如何使用以下内容的正确方向吗:

curl "https://api.xxxxxx.com/phones/:number/calls" \
-d"call[phone_number]=<number-to-call>" \
-d"token=<api_token>" \
--insecure

我试图找到答案,但我似乎无法得到任何对我有用的东西 - 它应该让电话响起!!!理论上!但是找不到用 PHP 发送请求的方法。

谢谢, B.

【问题讨论】:

    标签: php api curl


    【解决方案1】:

    像这样使用 PHP 的 cURL:

    <?php
    $ch = curl_init();
    
    $data = array(
      'call[phone_number]' => '<number-to-call>', 
      'token' => '<api_token>'
    );
    
    curl_setopt($ch, CURLOPT_URL, 'https://api.xxxxxx.com/phones/:number/calls');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    
    curl_exec($ch);
    ?>
    

    【讨论】:

      【解决方案2】:

      我总是花太长时间来发布大声笑是的对不起:D

      <?php     
      
          //optional comment out or delete    
          error_reporting(E_ALL);    
      
          // The POST URL and parameters    
          $request =  'http://world.needforspeed.com/SpeedAPI/ws/game/nfsw/driver/'.$u.'/profile?output=xml';    
      
          // Get the curl session object    
          $session = curl_init($request);    
      
          // Set the POST options.     
          curl_setopt($session, CURLOPT_HEADER, true);    
          curl_setopt($session, CURLOPT_RETURNTRANSFER, true);    
      
          // Do the POST and then close the session    
          $response = curl_exec($session);    
          curl_close($session);    
      
          // Get HTTP Status code from the response    
          $status_code = array();    
          preg_match('/\d\d\d/', $response, $status_code);    
      
          // Check for errors    
          switch( $status_code[0] ) {    
              case 200:    
                  // Success    
                  break;    
              case 503:    
                  die('Service unavailable. An internal problem prevented us from returning data to you.');    
                  break;    
              case 403:    
                  die('Forbidden. You do not have permission to access this resource, or are over your rate limit.');    
                  break;    
              case 400:    
                  // You may want to fall through here and read the specific XML error    
                  die('Bad request. The parameters passed to the service did not match as expected. The exact error is returned in the XML response.');    
                  break;    
              default:    
                  die('Your call returned an unexpected HTTP status of:' . $status_code[0]);    
          }    
      
          // Get the XML from the response, bypassing the header    
          if (!($xml = strstr($response, '<?xml'))) {    
              $xml = null;    
          }    
      
          // Output the XML    
      
          $worldDriverprofile = simplexml_load_string($xml);    
      
              $playername = $worldDriverprofile['name'];    
              $playerlevel = $worldDriverprofile['level'];    
              $playermessage = $worldDriverprofile['statusMessage'];      
              $playerimage = $worldDriverprofile['image'];
              $playerdefault = $worldDriverprofile['defaultPersona'];
      
          echo "Driver Name: $playername <br />";    
          echo "Level: $playerlevel <br />";     
          echo "Message: $playermessage <br />";
      ?>
      

      这是用于 SpeedAPI 的 SpeedAPI,但您可以将其更改为您调用的 wahtever xml。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-01-16
        • 1970-01-01
        • 2010-12-20
        • 2012-05-26
        • 1970-01-01
        • 2020-05-13
        • 1970-01-01
        相关资源
        最近更新 更多