cocos2dx 使用curl

#include "HelloWorldScene.h"
#include "AppMacros.h"
#include "pthread.h"  
#include "Json.h"
#include "curl.h"
USING_NS_CC;


CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
//静态函数
size_t HelloWorld::writehtml(uint8_t* ptr,size_t size,size_t number,void *stream)    
{     
	CCString* a=CCString::createWithFormat("%s",ptr);  
	std::string str1=a->getCString();
	const char* _ch = str1.c_str();
	CCLog(_ch);
	Json* myjson = Json_create(_ch);
	const char* _ch1= Json_getString(myjson,"name","");
	CCLog(_ch1);

	return size*number;//这里一定要返回实际返回的字节数    
}
bool HelloWorld::init()
{
    //
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }

#if 1
	CURL *curl;  
	CURLcode res;  
	string cc; 
	curl=curl_easy_init();  
	if(curl)  
	{ 
		curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.148/phpProject/test1.php?user=wu"); //设置请求的地址  
		curl_easy_setopt(curl, CURLOPT_POST, true); //设置数据类型
		string caozuo="";
		curl_easy_setopt(curl, CURLOPT_POSTFIELDS,caozuo.c_str()); //将操作代码,和连接的网站组合,一起发送! 
		curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
		curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,&HelloWorld::writehtml); //数据处理回调函数  
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, &cc);//缓冲的内存  
		curl_easy_setopt(curl,CURLOPT_TIMEOUT_MS,5000);    //设置连接超时时间    
		res = curl_easy_perform(curl);  
		if(res==CURLE_OK)  
		{  
			CCLog("ok");
		}  
		curl_easy_cleanup(curl);  
	}  
	else  
	{  
		CCLog("curl is null");  
	}  
#endif
	

	return true;
}



void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
#endif
}

bool HelloWorld::ccTouchBegan( CCTouch* touch, CCEvent* event )
{
	CCLog("touchBegan1");
	return true;
}

<?php  
header("Access-Control-Allow-Origin: *");  
  
if(isset($_GET['user'])){  
//	echo "hello world";
 $_arr = array('id' => 1, 'name' => 'wuhongxing', 'age' => 20, 'sex' =>0);  
 $_arr = json_encode($_arr);  
 echo $_arr;  
}  
else{  
    echo "请先登录";  
}  
?> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值