Monday 12 August 2013

cURL php

cURL Functions

curl_close - ปิดกาลใช้ Functions curl
curl_copy_handle - สำเนาม้วนจัดการพร้อมกับการตั้งค่าของ
curl_errno - ส่งข้อผิดพลาด กลับมาเป็น เลข error เช่น error 404 file not FOUND
curl_error - กลับสตริงที่มีข้อผิดพลาดที่ผ่านมาสำหรับการใช้งานปัจจุบัน
curl_escape - URL encodes สตริงที่กำหนด
curl_exec - เซสชั่นการม้วน
curl_file_create - สร้างวัตถุ CURLFile
curl_getinfo - รับข้อมูลเกี่ยวกับการถ่ายโอนเฉพาะ
curl_init - เริ่มต้นเซสชั่นม้วน
curl_multi_add_handle - เพิ่มที่จับม้วนปกติที่จะจับหลายม้วน
curl_multi_close - ปิดชุดของม้วนจัดการ
curl_multi_exec - เรียกใช้การเชื่อมต่อย่อยของการจัดการม้วนปัจจุบัน
curl_multi_getcontent - กลับเนื้อหาของการจัดการม้วน CURLOPT_RETURNTRANSFER ถ้ามีการตั้งค่า
curl_multi_info_read - รับข้อมูลเกี่ยวกับการโอนปัจจุบัน
curl_multi_init - คืนใหม่จับหลายม้วน
curl_multi_remove_handle - ลบจับหลายจากชุดของม้วนจับ
curl_multi_select - รอให้การทำงานในการเชื่อมต่อ curl_multi ใด ๆ
curl_multi_setopt - ตั้งค่าตัวเลือกสำหรับการจับหลายม้วน
curl_multi_strerror - กลับรหัสข้อผิดพลาดสตริงอธิบาย
curl_pause - หยุดชั่วคราวยกเลิกการหยุดชั่วคราวและการเชื่อมต่อ
curl_reset - ตั้งค่าตัวเลือกทั้งหมดของการจัดการเซสชั่น libcurl
curl_setopt_array - ตั้งค่าหลายตัวเลือกสำหรับการถ่ายโอนม้วน
curl_setopt - ตั้งค่าตัวเลือกสำหรับการถ่ายโอนม้วน
curl_share_close - ปิดจับหุ้นม้วน
curl_share_init - เริ่มต้นจับหุ้นม้วน
curl_share_setopt - ตั้งค่าตัวเลือกสำหรับการจับหุ้นม้วน
curl_strerror - สตริงกลับอธิบายรหัสข้อผิดพลาดให้
curl_unescape - ถอดรหัส URL สตริงที่กำหนดเข้ารหัส
curl_version - ดูข้อมูลรุ่นม้วน

?>

    $urlWithoutProtocol = "www.domain.com:80/someServices";
    $request         = "";
    $isRequestHeader = false;

    $exHeaderInfoArr   = array();
    $exHeaderInfoArr[] = "Content-type: text/xml";
    $exHeaderInfoArr[] = "Authorization: "."Basic ".base64_encode("authen_user:authen_pwd");

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $urlWithoutProtocol);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    curl_setopt($ch, CURLOPT_HEADER, (($isRequestHeader) ? 1 : 0));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if( is_array($exHeaderInfo) && !empty($exHeaderInfo) )
    {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $exHeaderInfo);
    }
    $response = curl_exec($ch);
    curl_close($ch);

    echo $response;
?>
###########################################
$request = "format=csv&by=member&rs=hour&rk=productivity&rb=".$month_first_date."&re=".$cur_date = date('Y-m-d');
$urlWithoutProtocol = "www.rescuetime.com/anapi/data/?".$request ;
$isRequestHeader = FALSE;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $urlWithoutProtocol);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$productivity = curl_exec($ch);
curl_close($ch);

?>
#########################################
ถ้าเป็นเว็บไซต์ที่ใช้ https จะต้องเขียนอย่างนี้ครับ

$request = "format=csv&by=member&rs=hour&rk=productivity&rb=".$month_first_date."&re=".$cur_date = date('Y-m-d');
$urlWithoutProtocol = "https://www.rescuetime.com/anapi/data/?".$request ;
$isRequestHeader = FALSE;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $urlWithoutProtocol);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$productivity = curl_exec($ch);
curl_close($ch);

?>

########################################

Detect Redirection Based on Browser

In this first example, we will write a script that can detect URL redirections based on different browser settings. For example, some websites redirect cellphone browsers, or even surfers from different countries.
We are going to be using the CURLOPT_HTTPHEADER option to set our outgoing HTTP Headers including the user agent string and the accepted languages. Finally we will check to see if these websites are trying to redirect us to different URLs.
  1. // test URLs  
  2. $urls = array(  
  3.     "http://www.cnn.com",  
  4.     "http://www.mozilla.com",  
  5.     "http://www.facebook.com"  
  6. );  
  7. // test browsers  
  8. $browsers = array(  
  9.   
  10.     "standard" => array (  
  11.         "user_agent" => "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6 (.NET CLR 3.5.30729)",  
  12.         "language" => "en-us,en;q=0.5"  
  13.         ),  
  14.   
  15.     "iphone" => array (  
  16.         "user_agent" => "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A537a Safari/419.3",  
  17.         "language" => "en"  
  18.         ),  
  19.   
  20.     "french" => array (  
  21.         "user_agent" => "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB6; .NET CLR 2.0.50727)",  
  22.         "language" => "fr,fr-FR;q=0.5"  
  23.         )  
  24.   
  25. );  
  26.   
  27. foreach ($urls as $url) {  
  28.   
  29.     echo "URL: $url\n";  
  30.   
  31.     foreach ($browsers as $test_name => $browser) {  
  32.   
  33.         $ch = curl_init();  
  34.   
  35.         // set url  
  36.         curl_setopt($ch, CURLOPT_URL, $url);  
  37.   
  38.         // set browser specific headers  
  39.         curl_setopt($ch, CURLOPT_HTTPHEADER, array(  
  40.                 "User-Agent: {$browser['user_agent']}",  
  41.                 "Accept-Language: {$browser['language']}"  
  42.             ));  
  43.   
  44.         // we don't want the page contents  
  45.         curl_setopt($ch, CURLOPT_NOBODY, 1);  
  46.   
  47.         // we need the HTTP Header returned  
  48.         curl_setopt($ch, CURLOPT_HEADER, 1);  
  49.   
  50.         // return the results instead of outputting it  
  51.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  52.   
  53.         $output = curl_exec($ch);  
  54.   
  55.         curl_close($ch);  
  56.   
  57.         // was there a redirection HTTP header?  
  58.         if (preg_match("!Location: (.*)!"$output$matches)) {  
  59.   
  60.             echo "$test_name: redirects to $matches[1]\n";  
  61.   
  62.         } else {  
  63.   
  64.             echo "$test_name: no redirection\n";  
  65.   
  66.         }  
  67.   
  68.     }  
  69.     echo "\n\n";  
  70. }  

No comments:

Analytics and Statistic

Blog Archive