作者ldwang (地瓜)
看板PHP
標題[請益] 不知道是哪邊問題(curl?php版本?json?)
時間Thu Apr 18 12:32:54 2013
想要用廠商的API,他的範例CreatCustomers.php
我自己的appserv2.5.10跑出來,"Data"都null (如下)
result:{"Data":null,"ErrorCode":2,"Message":"FAIL"}
https://www.videgree.com/Store/Customer.mvc/Index 點它登入可看到您新增的!
我試著echo()出curl_init()的結果,會是Resource id #2,
echo()出base64_encode()的結果的話,會是
eyAgICAgICAgIkN1c3RvbWVyTm8iIDogIjEiLCAgICAgICAgICAgICAgICAi
Q3VycmVudE5hbWUiIDogIualiuadseWfjiIgICAgfQ==
我猜想是不是因為appserv的php5.2.6的關係?
好像是curl_setopt()還是curl_exec()這部分跑完之後,
應該要出來有東西的$resulta沒有東西。
可否給我一個方向去try?感謝!
http://www.videgree.com/store/ApiExample.mvc/Index/CreateCustomer
---------------------------------------------------------------------------------
class Util
{
public static function Post($URL, $data) { //Initiate cURL request and send
back the result
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:
application/json'));
curl_setopt($ch, CURLOPT_URL, $URL);
// curl_setopt($ch, CURLOPT_USERAGENT, $this->_agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
// curl_setopt($ch, CURLOPT_COOKIEFILE, $this->_cookie_file_path);
// curl_setopt($ch, CURLOPT_COOKIEJAR, $this->_cookie_file_path);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POST, 1);
$resulta = curl_exec($ch);
if (curl_errno($ch)) {
echo 'ch:' . $ch;
print curl_error($ch);
} else {
curl_close($ch);
}
return $resulta;
}
}
-------------------------------------------------------------------------------------
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.135.90.54
1F:→ dlikeayu:$url 你直接打在browser有print出東西嗎? 04/18 15:30
2F:→ dlikeayu:二來,按登入後 會在你action列出現的參數 你有用 04/18 15:32
3F:→ dlikeayu:urlencode decode去丟嗎? 04/18 15:33
4F:→ dlikeayu:繼第一點,$url的string開頭有http(s):// 的協定嗎? 04/18 15:34
5F:→ dlikeayu:剛剛把那base64拿去decode發現變成 04/18 15:37
6F:→ dlikeayu:{ "CustomerNo" : "1", "CurrentName":"楊東城"} 04/18 15:38
7F:→ dlikeayu:所以你試試看 return json_decode($resulta) 04/18 15:40
8F:→ dlikeayu:應該會得到一組array 04/18 15:40
9F:→ dlikeayu:大概知道你的問題點了 json_decode問題是正解 04/18 15:45
10F:→ dlikeayu:或json_decode($var, true); 04/18 15:46
已處理完畢
現在廠商那邊的範例稍微修改過了,已經可以正常運作了
原因只是name寫錯了(也不是寫錯,是整個程式裡都重新命名但漏了這裡)
文內我貼上來發問的程式碼,本身是可以run正確的。
如果您是也是用這產品的,現在的範例程式下載下來使用沒問題。(對server端的
名稱改正了)
※ 編輯: ldwang 來自: 220.135.90.54 (04/29 11:34)
11F:→ ldwang:dlikeayu非常感謝您。 04/29 11:36