向cURL发出请求时出错

时间:2017-08-04 14:01:43

标签: php curl

我想将数据发送到此卷曲

curl -X GET \
  -H "X-Parse-Application-Id: ${APPLICATION_ID}" \
  -H "X-Parse-REST-API-Key: ${REST_API_KEY}" \
  -G \
  --data-urlencode 'where={"name":{"$regex":"^Big Daddy"}}' \
  https://api.parse.com/1/classes/_User

到目前为止,我成功地制作了卷曲,但我无法整合--data-urlencode部分。谁能验证我的错误?感谢。

$str = $_POST['STR'];
$query = 'where={"name":{"$regex":"'.$str.'"}}';
$query = [
    "where" => '"name":{"$regex":"$str"}'
];
$url = "http://ec2-34-204-170-24.compute-1.amazonaws.com/parse/classes/_User";
$encoded_url = urlencode($url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($query));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$headers = [
    'X-Parse-Application-Id: YOLO',
    'X-Parse-REST-API-Key: 4314b22c8b68ed18cf42d'
];

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);

1 个答案:

答案 0 :(得分:0)

只需将其添加到网址中:

$url = "http://ec2-34-204-170-24.compute-1.amazonaws.com/parse/classes/_User?" . http_build_query($query);

请参阅:http://php.net/manual/en/function.http-build-query.php