简体   繁体   English

如何在 php 中将动态 URL 传递给 curl - 出现错误 1

[英]how to pass dynamic URL to curl in php - getting error 1

I'm trying to pass a $url to curl using a function. the URL is built with a variable in it in the following method:我正在尝试使用 function 将 $url 传递给 curl。URL 是通过以下方法构建的,其中包含一个变量:

a.php // main page, include (a.php, b.php)
b.php // dynamic string function
c.php // curl function

I build a dynamic string successfully using sessions data // $_SESSION["input"] myDynamicstringfunction set a string by multiple sessions input values.我使用会话数据成功构建了一个动态字符串 // $_SESSION["input"] myDynamicstringfunction 通过多个会话输入值设置一个字符串。

$dval = myDynamicstringfunction();
echo $dval;
// render correctly to: "-e5 -g6 -g7"

the $dval value is a string that resolve as expected. $dval 值是一个按预期解析的字符串。 the $url is: $url 是:

$url = "https://someurl.com/a/b?dc=-cv1.5 -a1 -b2 -c3 -d4 $dval";

The $url is render correctly with the $url 正确呈现

echo $url;
$url = "https://someurl.com/a/b?dc=-cv1.5 -a1 -b2 -c3 -d4 -e5 -g6 -g7";

I pass the $url to the curl function using:我使用以下方法将 $url 传递给 curl function:

$r = mycUrlfunction($url);

The curl function I use:我使用的 curl function:

function singleRequest($url){
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
$curly = curl_exec($ch);
if ($curly == FALSE){
    die("cURL Error: " . curl_error($ch));
}

$result = json_decode($curly, true);

// close cURL resource, and free up system resources
curl_close($ch);

echo '<pre>';
return ($result);
}

The above get me an error (curl 1) - CURLE_UNSUPPORTED_PROTOCOL (1)上面给我一个错误 (curl 1) - CURLE_UNSUPPORTED_PROTOCOL (1)

I have tried many things to get the $url to work with no success.我已经尝试了很多方法来让 $url 工作但没有成功。 if I set the $url value manually without the $dval variable like this:如果我像这样在没有 $dval 变量的情况下手动设置 $url 值:

$url = "https://someurl.com/a/b?dc=-cv1.5 -a1 -b2 -c3 -d4 -e5 -g6 -g7";

The code works just fine and I get the correct results from the API call.代码工作正常,我从 API 调用中得到了正确的结果。 I tried using different quotes, {}, [], encoding to ASCII, vprintf(), and other solutions with no success.我尝试使用不同的引号、{}、[]、编码为 ASCII、vprintf() 和其他解决方案,但均未成功。

the problem was with constructing the dynamic variable $dynamicstring of the URL initially used问题在于构造最初使用的 URL 的动态变量$dynamicstring

$dynamicstring= "-a" . $_SESSION["a"]." -b".$_SESSION['b']." -c".$_SESSION['c']." -d".$_SESSION['d']."<br>";

this have a few problems这有一些问题

  1. when using echo it render the expected output correctly使用 echo 时,它会正确呈现预期的 output
  2. it have a "<br>" at the end它最后有一个"<br>"
  3. it have the wrong structure它有错误的结构

the correct way to construct the $dynamicstring is to use {}构造$dynamicstring的正确方法是使用 {}

$dynamicstring= "-a{$_SESSION["a"]} -b{$_SESSION['b']} -c{$_SESSION['c']} -d{$_SESSION['d']}";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM