cURL操作和PHP

发布于 2024-12-08 19:34:53 字数 2321 浏览 0 评论 0原文

我有一个前端代码

$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_URL, $url);
//make the request
$responseJSON = curl_exec($ch);
$response_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($response_status == 200) { // success
    // remove any "problematic" characters from the json string and then decode
    if (debug) {
        echo "----finish API of getAPI inside basic_function with status==200---";
        echo "<br>";
        echo "-------the json response is-------" ;  //.$responseJSON;
        var_dump($responseJSON);
        //print_r($responseJSON);
        echo "<br>";
    }
    return json_decode( preg_replace( '/[\x00-\x1F\x80-\xFF]/', '', $responseJSON ) );
}

和一个后端代码,当 cURL 使用其 URL 触发其操作时执行该代码。因此,后端代码将被激活。所以,我知道 cURL 正在运行。

$output=array (
  'status'=>'OK',
  'data'=>'12345'
)

$output=json_encode($output)

echo $output;

$output 在浏览器上显示为 {"status":"OK","data":"12345"}

但是,我回到前端代码并执行了< code>echo $responseJSON,我什么也没得到。我认为 {"status":"OK","data":"12345"} 的输出会转到 $responseJSON。有什么想法吗?

这是浏览器上的输出,有些东西很奇怪!即使在后端代码解析 API 之前,response_status 也已达到 200,这就是成功的。我期望在 {"status":"OK","data":"12345"}

======================= 之后有 status =200 和 json 响应=================================================== ================

get API里面的基本功能

-------url of cURL is -----http://localhost/test/api/session/login/?device_duid=website&UserName=joe&Password=1234&Submit=Submit



----finish API of getAPI inside basic_function with status==200---
-------the json response is-------string(1153) 


"************inside Backend API.php******************

---command of api is--/session/login/


---first element of api is--UserName=joe

--second element of api is---Password=1234

---third element of api is----Submit=Submit

----fourth element of api is---

-------inside session login of api-------------

{"status":"OK","data":"12345"}

I have a frontend code

$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_URL, $url);
//make the request
$responseJSON = curl_exec($ch);
$response_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($response_status == 200) { // success
    // remove any "problematic" characters from the json string and then decode
    if (debug) {
        echo "----finish API of getAPI inside basic_function with status==200---";
        echo "<br>";
        echo "-------the json response is-------" ;  //.$responseJSON;
        var_dump($responseJSON);
        //print_r($responseJSON);
        echo "<br>";
    }
    return json_decode( preg_replace( '/[\x00-\x1F\x80-\xFF]/', '', $responseJSON ) );
}

and I have a backend code which executed when cURL fired its operation with its URL. The backend code would therefore activated. So, I know cURL is operating.

$output=array (
  'status'=>'OK',
  'data'=>'12345'
)

$output=json_encode($output)

echo $output;

and $output shown on browser as {"status":"OK","data":"12345"}

However, I gone back to the frontend code and did echo $responseJSON, I got nothing. I thought the output of {"status":"OK","data":"12345"} would gone to the $responseJSON. any idea?

Here's output on Browser, something is very odd! the response_status got 200 which is success even before the parsing of API by the backend code. I expect status =200 and json response after the {"status":"OK","data":"12345"}

=========================================================================================

inside the get API of the basic functions

-------url of cURL is -----http://localhost/test/api/session/login/?device_duid=website&UserName=joe&Password=1234&Submit=Submit



----finish API of getAPI inside basic_function with status==200---
-------the json response is-------string(1153) 


"************inside Backend API.php******************

---command of api is--/session/login/


---first element of api is--UserName=joe

--second element of api is---Password=1234

---third element of api is----Submit=Submit

----fourth element of api is---

-------inside session login of api-------------

{"status":"OK","data":"12345"}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只有影子陪我不离不弃 2024-12-15 19:34:53

您是否尝试过用 curl_setopt($ch, CURLOPT_TIMEOUT, 10); 评论?
如果您评论该行,看看会发生什么。

还尝试使用基本代码,如果有效,则您稍后添加的内容是错误的:

// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, false);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);

Have you tried with curl_setopt($ch, CURLOPT_TIMEOUT, 10); commented?
See what happends if you comment that line.

Also try with the a basic code, if that works, smthing you added later is wrong:

// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, false);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
只有一腔孤勇 2024-12-15 19:34:53

尝试 var_dump($responseJSON)

如果返回 false 尝试

curl_error($ch)

返回最后一个 cURL 操作的明文错误消息。

您确定您的 $url 正确吗?

Try var_dump($responseJSON)

If it returns false try

curl_error ( $ch )

Returns a clear text error message for the last cURL operation.

Are you sure your $url is correct?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文