php curl post数据获取值形式引用url [英] php curl post data to get value form referred url

查看:153
本文介绍了php curl post数据获取值形式引用url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向网页发送发布数据以获取价值。问题是,我想获得 abc.com 的数据形式。并且 abc.com 只有在用户来自 abc.com/时才打开 abc.com



也许它正在做一些会话或cookie来进行身份验证。我不知道。



我想做的是从abc.com获取数据。有没有办法通过curl来做呢?



我的PHP代码:



当你运行代码 c> c> =lang-php prettyprint-override> $ SearchBy =2;
$ AttorneySearchMode =Name;
$ LastName =Smith;
$ FirstName =William;
$ CaseStatusType =0;
$ SortBy =fileddate;


echo $ url ='https://www.clarkcountycourts.us/Anonymous/Search.aspx?ID=400&NodeID=101%2c103%2c104%2c105%2c500%2c600 %2c601%2c602%2c603%2c604%2c605%2c6​​06%2c607%2c608%2c609%2c610%2c611%2c612%2c613%2c614%2c615%2c6​​16%2c617%2c618%2c619%2c699%2c700%2c701%2c702%2c703%2c704 %2c705%2c706%2c707%2c708%2c709%2c710%2c711%2c712%2c713%2c714%2c715%2c716%2c717%2c718%2c719%2c720%2c721%2c722%2c723%2c724%2c725%2c726%2c727%2c728%2c729 %2c730%2c731%2c797%2c798& NodeDesc = All + Courts';

//设定POST变数
$ fields = array(
'SearchBy'=> urlencode($ SearchBy),
'AttorneySearchMode'=> urlencode $ AttorneySearchMode),
'LastName'=> urlencode($ LastName),
'FirstName'=> urlencode($ FirstName),
'CaseStatusType'=> urlencode ),
'SortBy'=> urlencode($ SortBy),
);
$ fields_string =;
foreach($ fields as $ key => $ value){
$ fields_string。= $ key。'='。$ value。'&';
}
rtrim($ fields_string,'&');
$ ch = curl_init();

curl_setopt($ ch,CURLOPT_URL,$ url);
curl_setopt($ ch,CURLOPT_POST,true);
curl_setopt($ ch,CURLOPT_POST,count($ fields));
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ fields_string);

$ result = curl_exec($ ch);
curl_close($ ch);
print_r($ result);


解决方案

/ p>

我发现有3个问题阻止我请求其服务器上的网页:


  1. 需要在发布之前启用Cookie并提取一个网页

  2. 需要停用SSL检查( CURLOPT_SSL_VERIFYPEER CURLOPT_SSL_VERIFYHOST

  3. 请求中缺少一些必要的表单字段

在下面的示例中,我首先获取主搜索页面,然后重新使用相同的cURL句柄来发布表单。注意这是不完整的,您需要编写代码来填充剩余的表单字段。

  //首先获取搜索页面cookies 
$ url ='https://www.clarkcountycourts.us/Anonymous/Search.aspx?ID=400&NodeID=101%2c103%2c104%2c105%2c500%2c600%2c601%2c602%2c603%2c604 %2c605%2c6​​06%2c607%2c608%2c609%2c610%2c611%2c612%2c613%2c614%2c615%2c6​​16%2c617%2c618%2c619%2c699%2c700%2c701%2c702%2c703%2c704%2c705%2c706%2c707%2c708 %2c709%2c710%2c711%2c712%2c713%2c714%2c715%2c716%2c717%2c718%2c719%2c720%2c721%2c722%2c723%2c724%2c725%2c726%2c727%2c728%2c729%2c730%2c731%2c797%2c798& ; NodeDesc = All + Courts';

$ ch = curl_init();

//为此站点禁用SSL检查
curl_setopt($ ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,0);

//启用cookie处理(它们在cURL关闭后不会被保存)
curl_setopt($ ch,CURLOPT_COOKIEFILE,'');

//调试
curl_setopt($ ch,CURLOPT_VERBOSE,1);
curl_setopt($ ch,CURLOPT_STDERR,fopen('php:// output','w'));

//有用的选项
curl_setopt($ ch,CURLOPT_AUTOREFERER,true);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,1);

//设置第一个URL
curl_setopt($ ch,CURLOPT_URL,$ url);

//执行第一个请求以建立cookie& referer
$ data = curl_exec($ ch);

// TODO:从表单中提取隐藏表单字段/值
$ fields_string ='...';

//现在做第二个请求
curl_setopt($ ch,CURLOPT_POST,true);
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ fields_string);

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

查看我的其他一些答案此处链接,其中显示了如何提取其他表单字段并执行多个请求。另请参阅( cURL - Cookie和会话)和( PHP Curl - Cookie问题



希望有帮助,请回答问题/问题。


I want to send post data to a page to get value. The problem is that I want to get data form abc.com. And abc.com opens only if user comes to abc.com from abc.com/previous url.

Maybe it is is doing some session or cookies to authenticate. I have no idea.

What I want to do is get data from abc.com. Is there any way via curl to do it?

My php code:

When you run the code it says url has be moved as for this url you need to come from old refer url then it works in browser

$SearchBy = "2";
$AttorneySearchMode="Name";
$LastName= "Smith";
$FirstName= "William";
$CaseStatusType= "0";
$SortBy= "fileddate";


echo $url = 'https://www.clarkcountycourts.us/Anonymous/Search.aspx?ID=400&NodeID=101%2c103%2c104%2c105%2c500%2c600%2c601%2c602%2c603%2c604%2c605%2c606%2c607%2c608%2c609%2c610%2c611%2c612%2c613%2c614%2c615%2c616%2c617%2c618%2c619%2c699%2c700%2c701%2c702%2c703%2c704%2c705%2c706%2c707%2c708%2c709%2c710%2c711%2c712%2c713%2c714%2c715%2c716%2c717%2c718%2c719%2c720%2c721%2c722%2c723%2c724%2c725%2c726%2c727%2c728%2c729%2c730%2c731%2c797%2c798&NodeDesc=All+Courts';

//set POST variables
$fields = array(
    'SearchBy' => urlencode($SearchBy),
    'AttorneySearchMode' => urlencode($AttorneySearchMode),
    'LastName' => urlencode($LastName),
    'FirstName' => urlencode($FirstName),
    'CaseStatusType' => urlencode($CaseStatusType),
    'SortBy' => urlencode($SortBy),
);
$fields_string = "";
foreach ($fields as $key=>$value) {
    $fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string, '&');
$ch = curl_init();

curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

$result = curl_exec($ch);
curl_close($ch);
print_r($result);

解决方案

Here is a starting point with some minor additions.

I found 3 issues that prevented me from requesting the page on their server:

  1. Need to enable cookies in cURL and fetch one page before posting
  2. Needed to disable SSL checks (CURLOPT_SSL_VERIFYPEER, CURLOPT_SSL_VERIFYHOST)
  3. Some necessary form fields are missing from the request

In the example below I first fetch the main search page, and then re-use the same cURL handle to post the form. Note this is incomplete, you will need to write code to populate the remaining form fields.

// first fetch search page to set cookies
$url = 'https://www.clarkcountycourts.us/Anonymous/Search.aspx?ID=400&NodeID=101%2c103%2c104%2c105%2c500%2c600%2c601%2c602%2c603%2c604%2c605%2c606%2c607%2c608%2c609%2c610%2c611%2c612%2c613%2c614%2c615%2c616%2c617%2c618%2c619%2c699%2c700%2c701%2c702%2c703%2c704%2c705%2c706%2c707%2c708%2c709%2c710%2c711%2c712%2c713%2c714%2c715%2c716%2c717%2c718%2c719%2c720%2c721%2c722%2c723%2c724%2c725%2c726%2c727%2c728%2c729%2c730%2c731%2c797%2c798&NodeDesc=All+Courts';

$ch = curl_init();

// needed to disable SSL checks for this site
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);

// enable cookie handling (they aren't saved after cURL is closed)
curl_setopt($ch,CURLOPT_COOKIEFILE, '');

// debugging
curl_setopt($ch,CURLOPT_VERBOSE, 1);
curl_setopt($ch,CURLOPT_STDERR, fopen('php://output', 'w'));

// helpful options
curl_setopt($ch,CURLOPT_AUTOREFERER, true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);

// set first URL
curl_setopt($ch,CURLOPT_URL, $url);

// execute first request to establish cookies & referer    
$data = curl_exec($ch);

// TODO: extract hidden form fields/values from form 
$fields_string = '...';

// now make second request
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

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

See some of my other answers linked here which shows how to extract other form fields and do multiple requests. See also (cURL -- cookies and sessions) and (PHP Curl - Cookies problem)

Hope that helps, comment back with issues/questions.

这篇关于php curl post数据获取值形式引用url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆