PHP CURL Mock Request ____php

Source: Internet
Author: User
Tags curl
Simulate a simple GET request sample: ==============================================
$url = "http://somedomain/ver/ffffffff-e31e-85d5-ffff-ffffa6220605/2/en/b0:79:94:e7:99:4a/3.5"; $ch = Curl_init ($url); Initialization of curl_setopt ($ch, Curlopt_header, 0); Do not return header part curl_setopt ($ch, Curlopt_returntransfer, true); Returns the string rather than the direct output curl_setopt ($ch, Curlopt_useragent, "dalvik/1.6.0" (Linux; U Android 4.1.2; DROID RAZR HD build/9.8.1q-62_vqw_mr-2) "); curl_setopt ($ch, Curlopt_referer, "-"); $response = curl_exec ($ch); Curl_close ($ch);
Print_r ($response);
It simulates the phone-side request, which sets the useragent to an Android phone. There are a lot of sites to determine where the request came from, with this can be mixed. At the same time, set the Referer to indicate where to jump to the current address, and some sites will make judgments.
simulate a POST login. And then do something else.============================================== Note: After the successful login, the Cookie will save a sessionID, followed by our access to other pages do not need to login because we visit the page will bring Cookies, so the service side uses the SessionID to determine if we are logged in. We need to log in again when the cookie expires. So when we simulate a login, we also need to save the cookie and then bring it to the next simulation
$account = "YourUserName"; $password = "YourPassword";
$post = "account= $account &password= $password";
First get the cookies and save the $cookie _file = dirname (__file__). /cookie.txt ';
$url = "Http://domain/login"; $ch = Curl_init ($url); Initialization of curl_setopt ($ch, Curlopt_header, 0); does not return header part curl_setopt ($ch, Curlopt_post, 1);//indicates that this request is a post curl_setopt ($ch, Curlopt_postfields, $post);/Set Post Parameter curl_setopt ($ch, Curlopt_returntransfer, true); Returns the string rather than the direct output curl_setopt ($ch, Curlopt_cookiejar, $cookie _file); Store Cookies curl_exec ($ch); Curl_close ($ch);
After the successful login, get the content of other pages of the user $url = "Http://domain/otherpage"; $ch = Curl_init ($url); curl_setopt ($ch, Curlopt_header, 0); curl_setopt ($ch, Curlopt_returntransfer, true); curl_setopt ($ch, Curlopt_cookiefile, $cookie _file); Use the cookies obtained above $response = curl_exec ($ch); Curl_close ($ch);
Print_r ($response);


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.