【问题标题】:Call external url with parameter in PHP在 PHP 中使用参数调用外部 url
【发布时间】:2018-06-07 07:17:29
【问题描述】:

我一直在努力使用参数从 PHP(提交时)调用外部 url。我看过不同的选项(curlfile_get_contents 等),但似乎没有任何效果。

最终 url 应该是这样的:

http://XXXXX:8080/job/Clone_CentOS_VM/buildWithParameters?token=smlvyKf6tS&vm_user=temp&memory_mb=1024&num_cpus=1&eth0_ip=172.XX.XX.XXX&eth1_ip=192.XX.XX.XX

加上参数,它看起来是这样的:

http://XXXXX:8080/job/Clone_CentOS_VM/buildWithParameters?token=smlvyKf6tS&vm_user=$login&memory_mb=$memory&num_cpus=$cpu&eth0_ip=$ip_172&eth1_ip=$ip_192";

【问题讨论】:

  • 试试 Guzzle github.com/guzzle/guzzle
  • 您的 PHP 代码错误!你没有逃脱你使用的变量!向我们展示您生成 URL 的代码。
  • 这对他的问题有什么帮助? @JaredChu

标签: php url curl


【解决方案1】:

您没有转义创建 URL 时使用的变量。如果你把变量放在一个字符串中,你需要告诉,变量是什么,字符串是什么。

例如,您可以在变量周围使用括号:

$url = "http://XXXXX:8080/job/Clone_CentOS_VM/buildWithParameters?token=smlvyKf6tS&vm_user={$login}&memory_mb={$memory}&num_cpus={$cpu}&eth0_ip={$ip_172}&eth1_ip={$ip_192}";

或者从字符串中排除它们:

$url = "http://XXXXX:8080/job/Clone_CentOS_VM/buildWithParameters?token=smlvyKf6tS&vm_user=" . $login . "&memory_mb=" . $memory . "&num_cpus=" . $cpu . "&eth0_ip=" . $ip_172 . "&eth1_ip=" . $ip_192;

【讨论】:

    【解决方案2】:

    您必须在字符串周围使用正确的引号字符" 而不是',否则您可以使用. 连接参数/字符串

    一个可能的解决方案是:

    $url = 'http://XXXXX:8080/job/Clone_CentOS_VM/buildWithParameters?token=smlvyKf6tS&vm_user='.$login.'&memory_mb='.$memory.'&num_cpus='.$cpu.'&eth0_ip='.$ip_172.'&eth1_ip='.$ip_192;
    

    【讨论】:

    • 正如您在他的示例中看到的,他使用了"(行尾)。
    • 如果有正确的引用",问题中给出的代码应该可以工作。因为在简单变量周围使用花括号不是强制性的。这仅对数组或对象是必需的,这里不是这种情况。 php.net/manual/en/…
    【解决方案3】:

    将您的网址构建为字符串,然后使用file_get_contents()

    $login = "userName";
    $memory = "memoryValue";
    $cpu = "cpuValue";
    $ip = "127_0_100_"; 
    
    $url = "http://XXXXX:8080/job/Clone_CentOS_VM/buildWithParameters?token=smlvyKf6tS&vm_user=" . $login . "&memory_mb=" . $memory . "&num_cpus=" . $cpu . "&eth0_ip=" . $ip . "_172&eth1_ip=" . $ip . "_192";
    
    $content = file_get_contents($url);
    

    【讨论】:

    • 你有点搞砸了 ip 变量。 ;)
    • @eisbehr 在原始字符串之后,它的 IP 由 _ 分隔,而不是传统的 . - 我试图尽可能地忠实于 URL 字符串示例。 :)
    • 我不知道你在哪里看,但请看他的示例网址。它是点注释的,只是变量名是$ip_172$ip_192
    【解决方案4】:

    使用所有定义参数作为字符串构建您的 url,然后使用 file_get_contents()curl()

    $url = "http://XXXXX:8080/job/Clone_CentOS_VM/buildWithParameters?token=smlvyKf6tS&vm_user=".$login."&memory_mb=".$memory."&num_cpus=".$cpu."&eth0_ip=".$ip_172."&eth1_ip=".$ip_192;
    
    1. file_get_contents()

      file_get_contents($url);

    2. curl()

              $request_headers[] =  'Content-Type:application/json';
              if (!function_exists('curl_init')){
                  die('cURL is not installed. Install and try again.');
              }
              $ch = curl_init();
              curl_setopt($ch, CURLOPT_URL, $url);
              curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
              curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
              curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
              curl_setopt($ch, CURLOPT_TIMEOUT, 30);
              $result = curl_exec($ch);
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-04
      • 1970-01-01
      • 2016-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-23
      相关资源
      最近更新 更多