PHP Request Service Curl and JSON parsing

Source: Internet
Author: User
Tags urlencode

For the thinkphp framework, I believe every PHP developer will know or be familiar with it! Front end a lot of use of the combination of Ajax, a few days ago encountered a problem, is to request another service, that is, request an interface, and then the other side returned a JSON string, the beginning of the curl is not familiar with, resulting in their own program can not be very smooth to handle the request service after the JSON string! Today to curl another look, the problem solved, and now the requested code to do the following display:
$new _url= "www.xxx.com";
$requesturl = "Http://192.168.1.100:1234/test/test?url=". UrlEncode ($new _url);
$ch = Curl_init ();
Set the URL and the appropriate options
curl_setopt ($ch, Curlopt_url, $requesturl);
curl_setopt ($ch, Curlopt_header, 0);
curl_setopt ($ch, Curlopt_returntransfer, 1);
$info =curl_exec ($ch);
Turn off the Curl resource and release the system resources
Curl_close ($ch);
$info to the content after the service is requested, $info will accept different content depending on the parameters set, in short, you need to know the results you want, and then configure the options. $info is not necessarily a JSON string, it depends on the function of the service. If you request the service, the other side will return you a JSON string, and you need to decompose the JSON string, then do a bit of work, you can get each key and value:
Suppose we $info = ' {' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4, ' E ': 5} ';
Decode the JSON string that is returned and, after decomposition, synthesizes the JSON string with the following array
$info = Json_decode ($info, true);
$a = $info [' a '];
$b = $info [' B '];
.....
This allows you to get each key and value. As for what you are going to do, it is your own business!



Second update: November 30, 2012 11:50:15

Because there was not much contact with PHP before, there is another way to display the service request:
$new _url= "www.xxx.com";
$requesturl = "Http://192.168.1.100:1234/test/test?url=". UrlEncode ($new _url);
         $json _ret = file_get_contents ($requesturl);
         $json _ret = mb_convert_encoding ($json _ret, "UTF-8", "gb2312") ;//transcoding, (here is just an example)
         $result = Json_decode ($json _ret, true);// The JSON string is decoded, provided that the return is returned after being processed by the Json_encode.

When using the File_get_contents function to collect the website, sometimes encounter the browser can see clearly, but is not the content of the problem. Let's talk about the cause of the problem and how to solve it. Now let's talk about User-agent. User-agent is part of the HTTP protocol and is part of the header domain, and in layman's terms, user-agent is an identification of the type of browser you use, the operating system, the browser kernel, and so on, to access the site. With this logo, the websites visited by the user can display different layouts to provide a better experience for the user or to perform information statistics. For example, the same site (page) with the PC and mobile terminal access to show the page layout and other information is not the same. Here are some of the problems we have seen: when using the file_get_contents function to collect the site, sometimes you will encounter the browser can see, but is not the content of the problem. This is probably because the server has made the settings, according to User_agent to determine whether the normal browser request, the default PHP file_get_contents function is not to send UA, if you want to collect such a site, we have to let the PHP simulation browser to send UA, So the other side of the server will think we are browsing with the browser, and return to normal content.
Sending the UA method is simple, add this sentence before using the file_get_contents function:
ini_set(‘user_agent‘,‘Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3‘);For Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 , you can modify the other as needed. With such a header setting, there is no such problem. This is what is usually said in disguise.

Json_encode ()/json_decode () Learning Links:
http://blog.csdn.net/nailwl/article/details/5687363
Http://cn2.php.net/json_decode
The Curl option in PHP is set:
Http://www.cnblogs.com/c3055/archive/2011/08/18/2143832.html
Http://www.wapm.cn/phpdoc/zh/ref.curl.html
Ini_set ():
Http://hi.baidu.com/bing2liuliu/item/ee3abcbe6a302547ba0e12a0

Wampserver Some of the curl extensions are not selected, please click:
http://blog.sina.com.cn/s/blog_6ce9e8870101bq0r.html for configuration

PHP Request Service Curl and JSON parsing

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.