0

I have to post values having single quote inside the string to a url using curl, single quote automatically stripped I think, how do I properly include single quote in strings while using curl,

$url = "test.com/req?a=10&b='1010','1012'";  
$ch = curl_init();  
curl_setopt($ch, CURLOPT_URL, $url);  
curl_setopt($ch, CURLOPT_HEADER, 0);  
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
$result = curl_exec($ch);  
curl_close($ch);  
4

2 回答 2

2

Use urlencode.

$url = "test.com/req?a=10&b=" . urlencode("'1010','1012'");
于 2012-06-13T08:51:11.940 回答
0

这样您就可以构建任何复杂的查询字符串:

$url   = 'test.com/req';
$query = array(
  'a' => 10,
  'b' => "'1010','1012'"
);

$url .= '?'. http_build_query($query);

// will produce this
// test.com/req?a=10&b=%271010%27%2C%271012%27
于 2012-06-13T09:12:41.963 回答