做一个卷曲请求

时间:2015-01-24 09:38:52

标签: php curl

我正在尝试在网站上执行卷曲请求,但我似乎无法使其正常工作。

这就是我所拥有的

<?php
 $mynumber = $_GET["mynumber"];
 echo $mynumber;
 $url = 'thewebsite/c/number='.$mynumber; // like this?
 $curl_header = array('X-Requested-With: XMLHttpRequest');
 $ch = curl_init($url);
 curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_header);
 curl_setopt($ch, CURLOPT_POSTFIELDS, "number=$mynumber"); //??? --data     "number=12345678" ?
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_COOKIE, $cookie);
 curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
 curl_setopt($ch, CURLOPT_VERBOSE, true);
 $result = curl_exec($ch);
 echo $result;
 curl_close($ch)
 ?>

这是卷曲请求:

"Content-Type: application/x-www-form-urlencoded; charset=UTF-8" -H "Accept: application/json, text/javascript, */*; q=0.01" -H "Referer: thewebsite" -H "X-Requested-With: XMLHttpRequest" -H "Connection: keep-alive" --data "number=12345678" --compressed  POST /thewebsite/c HTTP/1.1

我不知道如何传递“--data”number = 12345678“

1 个答案:

答案 0 :(得分:3)

您好像正确传递了--data参数。但您忘记设置Content-TypeAcceptRefererConnection标头。您可以按照设置X-Requested-With标题($curl_header)中的方式添加:

<?php

$cookie = '';
$mynumber = $_GET['mynumber'];

$ch = curl_init('http://example.com/c/');

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'X-Requested-With: XMLHttpRequest',
    'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
    'Accept: application/json, text/javascript, */*; q=0.01',
    'Referer: http://example.com/c/',
    'Connection: keep-alive'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, "number=$mynumber");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, true);

$result = curl_exec($ch);
var_dump($result);