2016-07-04 21 views
0

私はPHPでデータのスクレイピングを試みていますが、アクセスする必要のあるURLには投稿データが必要です。私は、実際の情報は、それが応答データをロードに失敗しまし返すホストされている2番目の$ URLにアクセスしようとしたが、それは私がNCAAホームページにアクセスできるようになりますPHP cURLが応答データを読み込めませんでした

<?php 

//set POST variables 
$url = 'https://www.ncaa.org/'; 
//$url = 'https://web3.ncaa.org/hsportal/exec/hsAction?hsActionSubmit=searchHighSchool'; 

// This is the data to POST to the form. The KEY of the array is the name of the field. The value is the value posted. 
$data_to_post = array(); 
$data_to_post['hsCode'] = '332680'; 
$data_to_post['state'] = ''; 
$data_to_post['city'] = ''; 
$data_to_post['name'] = ''; 
$data_to_post['hsActionSubmit'] = 'Search'; 

// Initialize cURL 
$curl = curl_init(); 

// Set the options 
curl_setopt($curl,CURLOPT_URL, $url); 

// This sets the number of fields to post 
curl_setopt($curl,CURLOPT_POST, sizeof($data_to_post)); 

// This is the fields to post in the form of an array. 
curl_setopt($curl,CURLOPT_POSTFIELDS, $data_to_post); 

//execute the post 
$result = curl_exec($curl); 

//close the connection 
curl_close($curl); 

?> 

。正しいフォームデータを送信していても応答データを読み込めないという理由がありますか?

+0

それはあなたが、私はそれの例を持っていることは、ポスト –

+0

を投稿しない必要と思われますどうもありがとうございました。これは私が必要とするように正確に動作します。これはphpのcURLに関する私の研究に役立ちます。 – Cesarg219

+0

@SunilPachlangia – Cesarg219

答えて

1

このサイトは、認識されたユーザーエージェントを確認しているようです。デフォルトでは、PHP curlはUser-Agentヘッダーを送信しません。追加

curl_setopt($curl, CURLOPT_USERAGENT, 'curl/7.21.4'); 

このスクリプトは応答を返します。しかし、この場合、あなたが持っているブラウザーよりも新しいブラウザーが必要であると答えています。したがって、実際のブラウザからユーザーエージェント文字列をコピーする必要があります。

curl_setopt($curl, CURLOPT_USERAGENT, '"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'); 

また、パラメータはapplication/x-www-form-urlencoded形式で送信する必要があります。 CURLOPT_POSTFIELDSへの引数として配列を使用すると、multipart/form-dataが使用されます。その行を次のように変更します。

curl_setopt($curl,CURLOPT_POSTFIELDS, http_build_query($data_to_post)); 

URLエンコードされた文字列に配列を変換します。

URLには、?hsActionSubmit=searchHighSchoolのように、そのパラメータがPOSTフィールドに送信されます。

最終、作業スクリプトは次のようになります。

<?php 
//set POST variables 
//$url = 'https://www.ncaa.org/'; 
$url = 'https://web3.ncaa.org/hsportal/exec/hsAction'; 

// This is the data to POST to the form. The KEY of the array is the name of the field. The value is the value posted. 
$data_to_post = array(); 
$data_to_post['hsCode'] = '332680'; 
$data_to_post['state'] = ''; 
$data_to_post['city'] = ''; 
$data_to_post['name'] = ''; 
$data_to_post['hsActionSubmit'] = 'Search'; 

// Initialize cURL 
$curl = curl_init(); 

// Set the options 
curl_setopt($curl,CURLOPT_URL, $url); 

// This sets the number of fields to post 
curl_setopt($curl,CURLOPT_POST, sizeof($data_to_post)); 

// This is the fields to post in the form of an array. 
curl_setopt($curl,CURLOPT_POSTFIELDS, http_build_query($data_to_post)); 
curl_setopt($curl, CURLOPT_USERAGENT, '"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'); 
//execute the post 
$result = curl_exec($curl); 

//close the connection 
curl_close($curl); 
+0

かかわらず、それを送ることが高校のコードでは、ウェブサイトや種類を表示した場合、GETメソッドは – Cesarg219

+0

申し訳ありませんが、今はスクリプトが機能していません。 – Cesarg219

+0

何も変更されていないのはなぜですか? – Cesarg219

0

カールHTTPS接続がspecicalオプションをオフにする必要があります。 2番目のURLを見て CURLOPT_SSL_VERIFYPEER

// Initialize cURL 
$curl = curl_init(); 

// Set the options 
curl_setopt($curl,CURLOPT_URL, $url); 

// ** This option MUST BE FALSE ** 
**curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);** 

// This sets the number of fields to post 
curl_setopt($curl,CURLOPT_POST, sizeof($data_to_post)); 

// This is the fields to post in the form of an array. 
curl_setopt($curl,CURLOPT_POSTFIELDS, $data_to_post); 

//execute the post 
$result = curl_exec($curl); 

//close the connection 
curl_close($curl); 
関連する問題