发送JSON由卷曲总是返回[否访问" [英] Send JSON by cURL always returns "No access"

查看:177
本文介绍了发送JSON由卷曲总是返回[否访问"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的JSON数据:

  $。阿贾克斯({
    键入:GET,
    网址:http://www.example.com/test.php
    数据:code = Sh9QA和放大器;令牌= 0982ff3066a3c60dbd3ecf9bcafc801b
    的contentType:应用/ JSON的;字符集= UTF-8,
});
 

要将此数据发送到 http://www.example.com/test.php ,我曾尝试与该code:

 < PHP

// API网址
$ URL ='http://www.example.com/test.php';

//启动卷曲。
$ CH = curl_init($网址);

// JSON数据。
$ jsonData =阵列(
    '数据'=> code = Sh9QA和放大器;记号= 0982ff3066a3c60dbd3ecf9bcafc801b'
);

// EN $ C C数组到JSON $。
$ jsonDataEn codeD = json_en code($ jsonData);

//告诉卷曲,我们想要发送POST请求。
curl_setopt($沟道,CURLOPT_POST,1);

//附上我们的EN codeD JSON字符串的POST领域。
curl_setopt($ CH,CURLOPT_POSTFIELDS,$ jsonDataEn codeD);

//设置内容类型的应用程序/ JSON
curl_setopt($ CH,CURLOPT_HTTPHEADER,阵列(内容类型:应用程序/ JSON'));

//执行请求
$结果= curl_exec($ CH);

?>
 

但是,它总是retuns 无访问

什么是错误的,我code?你能不能帮我解决这个问题?


  

对不起我的英语,这是不好的。如果我的我的问题是不明确的,请在下面评论这个问题。

解决方案

没有看出来我唯一可以建议是从数组删除数据,只是使文档是关键 code

 < PHP

// API网址
$ URL ='http://www.example.com/test.php';
$数据=?code = Sh9QA和放大器;记号= 0982ff3066a3c60dbd3ecf9bcafc801b

//启动卷曲。
$ CH = curl_init();
curl_setopt($ CH,CURLOPT_URL,$ URL $的数据);
curl_setopt($沟道,CURLOPT_RETURNTRANSFER,1);

//执行请求
$结果= curl_exec($ CH);

?>
 

I have this JSON data:

$.ajax({
    type: "GET",
    url: "http://www.example.com/test.php",
    data:"code=Sh9QA&token=0982ff3066a3c60dbd3ecf9bcafc801b",
    contentType: "application/json; charset=utf-8",
});

To send this data to http://www.example.com/test.php, I have tried with this code:

<?php

//API URL
$url = 'http://www.example.com/test.php';

//Initiate cURL.
$ch = curl_init($url);

//The JSON data.
$jsonData = array(
    'data' => 'code=Sh9QA&token=0982ff3066a3c60dbd3ecf9bcafc801b'
);

//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);

//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);

//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 

//Execute the request
$result = curl_exec($ch);

?>

But, it always retuns No access.

What are wrong in my code? Can you help me to fix it?


Sorry about my English, it is not good. If my my question is not clear, please comment below this question.

解决方案

Without the documentation to look out the only thing I can suggest is to remove the data from the array and just make it key code.

<?php

//API URL
$url = 'http://www.example.com/test.php';
$data = "?code=Sh9QA&token=0982ff3066a3c60dbd3ecf9bcafc801b"

//Initiate cURL.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//Execute the request
$result = curl_exec($ch);

?>

这篇关于发送JSON由卷曲总是返回[否访问&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆